Realm-java: "Realm instance has been closed" exception on un-closed Realm

Created on 16 May 2017  ·  11Comments  ·  Source: realm/realm-java

@cmelchior and @zhuinden recall quirky experiences with RealmCache causing an open Realm to appear closed.

The issue arose, recently in #4641

This issue tracks identifying and fixing the bug

T-Bug

Most helpful comment

Note, I don't think it is related to the RealmCache, but to the activity lifecycle. If I remember correctly last I checked, it appeared that sometimes things like ListView/RecyclerView would get invalidated during onDestroy() which would force a redraw and then naturally crash since the Realm was closed and data not available. I did not get to the bottom of it though.

All 11 comments

@zaki50 suggests that this may be related to #4551

@bmeike No, since #4551 is only in the master branch and not yet released.
Sorry for confusing.

I wanted to find this odd behavior a while ago to pinpoint it if there truly is an issue somewhere - all I know is that Realm instance doesn't "auto-close" unless you actually call close() on a Realm instance somewhere. But I don't know if calling close() on one instance can truly affect other instance.

I've been using a single UI-thread Realm instance since, but that's not really a solution :stuck_out_tongue:

Note, I don't think it is related to the RealmCache, but to the activity lifecycle. If I remember correctly last I checked, it appeared that sometimes things like ListView/RecyclerView would get invalidated during onDestroy() which would force a redraw and then naturally crash since the Realm was closed and data not available. I did not get to the bottom of it though.

@cmelchior Shall I move this issue to #adapters?

I also encountered the same problem,is there a good solution?

@bmeike do we have any reproduce steps for this? After checking the original issue, it looks very much something wrong in the user's code with controlling Realm life-cycles.

I think it was all caused by just that RealmResults and its Change listener is not invalidated / removed when activity.onDestroy is called so if a change happens after activity dies, the RealmResults is not guaranteed to be dead and can still try to update adapter even though it is a dead view hierarchy

Also Realm should be closed only after super.onDestroy because of some accessibility magic

@beeender so it is as if even though local instance is closed, then RealmResults is invalid even if global instance is open.

should be a user issue, don't see there is an issue with RealmCache. close it.

Was this page helpful?
0 / 5 - 0 ratings