java.lang.IllegalStateException: Cannot use ImplicitTransaction after it or its parent has been closed.
java.lang.IllegalStateException: Cannot use ImplicitTransaction after it or its parent has been closed.
at io.realm.internal.ImplicitTransaction.assertNotClosed(ImplicitTransaction.java:66)
at io.realm.internal.ImplicitTransaction.advanceRead(ImplicitTransaction.java:29)
at io.realm.Realm$RealmCallback.handleMessage(Realm.java:194)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:176)
I'm getting the above error while adding/updating realm objects in a background handler thread. What is the reason behind this?
You have called close() on Realm and are probably trying to access a RealmResult or RealmObject afterwards.
Is this the only possibility? Because I don't see this happening anywhere in my code.
It is by far the most likely. We will need more information to be more specific, can you share your code or try to reduce it to a testcase?
Wait, encountering it again. Below is an explanation of what I'm trying to do.
I need to constantly update the app with data from the server. So for polling data, I created a handler thread with a Looper. On app start, in the run() method of the Runnable, I get a Realm instance, perform a transaction, close the Realm instance, and then post the Runnable again to the same thread with a delay of say, 60 seconds.
Below is some pseudo code:
Runnable r = new Runnable() {
@Override
public void run() {
httpRequest.get(uri, new DataCallback() {
public void onSuccess(JSONObject result) throws JSONException {
Realm realm = Realm.getInstance(context);
realm.setAutoRefresh(true);
try {
realm.beginTransaction();
<Some write with data from the server>
realm.commitTransation();
} catch (Exception e) {
realm.cancelTransaction();
}
realm.close();
}
}
handlerThread.postDelayed(r, 60 * 1000);
}
}
handlerThread.post(r) // executed at app start.
I post some other Runnables too on the same handler thread. Some of them involve a network call, some of them just work with local data.
That pattern looks pretty solid. You just have to be careful about catching Exception, as you might catch OutOfMemory which could potentially cause corruption on your database. Catching RuntimeException is much safer.
Is it inside that exact runnable you get the ImplicitTransaction exception?
@ImGauravC What kind of writes are doing. Only writes on new objects or overriding objects that came from somewhere else?
@cmelchior : I sync current client state with the server response. So it involves either:
a) using setters to change values of RealmObjects.
b) creating new RealmObjects
c) Since some data might be deleted on the server, so in order to keep the data in sync on the client, I delete all realm objects using realm.clear(
Oh, and the objects might be in use in activity/adapter while this is being done.
You a) might be causing the problems. Realm instances are cached on the same thread and reference counted, but if you reference count reaches 0, it will close the Realm and all objects are no longer valid.
So if you access RealmObjects you have not queried for in the success handler you might risk they have been closed.
@cmelchior : I do query for the objects in the success handler. Still facing the issue.
In that case I would be very interested to know exactly what your code does as you might have stumbled upon a bug somewhere. Are you in a position to share it or can you reduce it to a minimal case?
Just a note. I just found a bug in our our Realm.close() code that could cause it to behave wrong if you call close to many times, so I would check for that.
@ImGauravC I found another bug in how our Realm.close() method works on background threads which could explain the behaviour you are seeing. See the above PR for a fix.
Great! Can we have the 0.77 soon?
Sure - we hope to cut it tomorrow all going well :-)
Fix has been merged into master and will be part of 0.77.0
Hi @cmelchior I'm still seeing this.
java.lang.IllegalStateException: Cannot use ImplicitTransaction after it or its parent has been closed.
at io.realm.internal.ImplicitTransaction.assertNotClosed(ImplicitTransaction.java:66)
at io.realm.internal.ImplicitTransaction.advanceRead(ImplicitTransaction.java:29)
at io.realm.Realm$RealmCallback.handleMessage(Realm.java:212)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:137)
I've multiple background threads fetching data from the server for different parts of the application (Just in case this might help.)
Also with the newest 0.78 release?
Didn't check this one
Yes, even with the new version.
Hi @ImGauravC Are you still seeing this issue with the latest version of Realm?