Search in sources :

Example 1 with RealmObjectObserver

use of chat.rocket.persistence.realm.RealmObjectObserver in project Rocket.Chat.Android by RocketChat.

the class MethodCall method execute.

/**
   * insert a new record to request a method call.
   */
public static Task<String> execute(RealmHelper realmHelper, String name, String paramsJson, long timeout) {
    final String newId = UUID.randomUUID().toString();
    TaskCompletionSource<String> task = new TaskCompletionSource<>();
    realmHelper.executeTransaction(realm -> {
        MethodCall call = realm.createObjectFromJson(MethodCall.class, new JSONObject().put(ID, newId).put(SYNC_STATE, SyncState.NOT_SYNCED).put(TIMEOUT, timeout).put(NAME, name));
        call.setParamsJson(paramsJson);
        return null;
    }).continueWith(_task -> {
        if (_task.isFaulted()) {
            task.setError(_task.getError());
        } else {
            final RealmObjectObserver<MethodCall> observer = realmHelper.createObjectObserver(realm -> realm.where(MethodCall.class).equalTo(ID, newId));
            observer.setOnUpdateListener(methodCall -> {
                if (methodCall == null) {
                    observer.unsub();
                    REF_MAP.remove(newId);
                    return;
                }
                int syncState = methodCall.getSyncState();
                RCLog.d("MethodCall[%s] syncstate=%d", methodCall.getMethodCallId(), syncState);
                if (syncState == SyncState.SYNCED) {
                    String resultJson = methodCall.getResultJson();
                    if (TextUtils.isEmpty(resultJson)) {
                        task.setResult(null);
                    } else {
                        task.setResult(resultJson);
                    }
                    observer.unsub();
                    REF_MAP.remove(methodCall.getMethodCallId());
                    remove(realmHelper, methodCall.getMethodCallId()).continueWith(new LogcatIfError());
                } else if (syncState == SyncState.FAILED) {
                    task.setError(new Error(methodCall.getResultJson()));
                    observer.unsub();
                    REF_MAP.remove(methodCall.getMethodCallId());
                    remove(realmHelper, methodCall.getMethodCallId()).continueWith(new LogcatIfError());
                }
            });
            observer.sub();
            REF_MAP.put(newId, observer);
        }
        return null;
    });
    return task.getTask();
}
Also used : RealmObject(io.realm.RealmObject) TextUtils(android.text.TextUtils) HashMap(java.util.HashMap) UUID(java.util.UUID) RCLog(chat.rocket.android.log.RCLog) RealmObjectObserver(chat.rocket.persistence.realm.RealmObjectObserver) TaskCompletionSource(bolts.TaskCompletionSource) PrimaryKey(io.realm.annotations.PrimaryKey) JSONObject(org.json.JSONObject) RealmHelper(chat.rocket.persistence.realm.RealmHelper) SyncState(chat.rocket.core.SyncState) LogcatIfError(chat.rocket.persistence.realm.helpers.LogcatIfError) Task(bolts.Task) TaskCompletionSource(bolts.TaskCompletionSource) JSONObject(org.json.JSONObject) LogcatIfError(chat.rocket.persistence.realm.helpers.LogcatIfError) LogcatIfError(chat.rocket.persistence.realm.helpers.LogcatIfError)

Aggregations

TextUtils (android.text.TextUtils)1 Task (bolts.Task)1 TaskCompletionSource (bolts.TaskCompletionSource)1 RCLog (chat.rocket.android.log.RCLog)1 SyncState (chat.rocket.core.SyncState)1 RealmHelper (chat.rocket.persistence.realm.RealmHelper)1 RealmObjectObserver (chat.rocket.persistence.realm.RealmObjectObserver)1 LogcatIfError (chat.rocket.persistence.realm.helpers.LogcatIfError)1 RealmObject (io.realm.RealmObject)1 PrimaryKey (io.realm.annotations.PrimaryKey)1 HashMap (java.util.HashMap)1 UUID (java.util.UUID)1 JSONObject (org.json.JSONObject)1