Commit e05f3311 authored by Yusuke Iwaki's avatar Yusuke Iwaki

fix MethodCall observer not to be gabage collected.

parent eccd1b9e
...@@ -12,6 +12,7 @@ import chat.rocket.android.realm_helper.RealmObjectObserver; ...@@ -12,6 +12,7 @@ import chat.rocket.android.realm_helper.RealmObjectObserver;
import chat.rocket.android.service.RocketChatService; import chat.rocket.android.service.RocketChatService;
import io.realm.RealmObject; import io.realm.RealmObject;
import io.realm.annotations.PrimaryKey; import io.realm.annotations.PrimaryKey;
import java.util.HashMap;
import java.util.UUID; import java.util.UUID;
import org.json.JSONObject; import org.json.JSONObject;
import timber.log.Timber; import timber.log.Timber;
...@@ -89,6 +90,8 @@ public class MethodCall extends RealmObject { ...@@ -89,6 +90,8 @@ public class MethodCall extends RealmObject {
} }
} }
private static final HashMap<String, RealmObjectObserver<MethodCall>> refMap = new HashMap<>();
/** /**
* insert a new record to request a method call. * insert a new record to request a method call.
*/ */
...@@ -122,14 +125,17 @@ public class MethodCall extends RealmObject { ...@@ -122,14 +125,17 @@ public class MethodCall extends RealmObject {
task.setResult(resultJson); task.setResult(resultJson);
} }
observer.unsub(); observer.unsub();
refMap.remove(methodCall.getMethodCallId());
remove(realmHelper, methodCall.getMethodCallId()).continueWith(new LogcatIfError()); remove(realmHelper, methodCall.getMethodCallId()).continueWith(new LogcatIfError());
} else if (syncstate == SyncState.FAILED) { } else if (syncstate == SyncState.FAILED) {
task.setError(new Error(methodCall.getResultJson())); task.setError(new Error(methodCall.getResultJson()));
observer.unsub(); observer.unsub();
refMap.remove(methodCall.getMethodCallId());
remove(realmHelper, methodCall.getMethodCallId()).continueWith(new LogcatIfError()); remove(realmHelper, methodCall.getMethodCallId()).continueWith(new LogcatIfError());
} }
}); });
observer.sub(); observer.sub();
refMap.put(newId, observer);
if (context != null) { if (context != null) {
RocketChatService.keepalive(context); RocketChatService.keepalive(context);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment