Realm-java: Can't close realm instance

Created on 18 Aug 2016  路  10Comments  路  Source: realm/realm-java

I'm having problem closing a realm instance. I'm doing some test, my code is the following:

Realm realm = Realm.getDefaultInstance();
Log.d("TEST1", "realm is "+(realm.isClosed()?"closed":"open"));
realm.close();
new Handler().postDelayed(()->{
    Log.d("TEST2", "realm is "+(realm.isClosed()?"closed":"open"));
    Log.d("TEST3", "query: "+realm.where(RealmBadge.class).findFirst().getPoints());
}, 3000);

output:

TEST1: realm is open
TEST2: realm is open
TEST3: query: 1

so realm is not closed by the .close() function. I'm using an handler only to test if the realm instance closes in some second, but nothing changes removing it.

I've also noticed that if after some time the realm instance closes I can't get an opened one simply by doing realm = Realm.getDefaultInstance() (the returned instance is already closed and return true for realm.isClosed())

T-Help

Most helpful comment

Currently I'm not having any problem, realm.close() is working as expected for me. I was having some threading problem (realm wasn't so good at that time, and I wasn't using it in the correct way). Normally I get the instance in the onCreate and close it in the onDestroy (or onStart and onStop with fragments). If realm is not closing, you probably did Realm.getDefaultInstance() more times than realm.close()

All 10 comments

So where else do you open new Realm instances?

After your comment i've noticed that I was missing a close on a prevjously executed thread that was creating too much instances. So I've figured out that there's a limit to the realm instances, is that correct? Instead I must have other problem with my code

There is no limit, just reference counting. However, leaving a background thread realm open is a very severe error.

As @Zhuinden points out, leaving a background thread with an open Realm is problematic: https://realm.io/docs/java/latest/#large-realm-file-size

Sure, that was just an oversight, now I have something like

Realm realm = Realm.getDefaultInstance();
realm.executeTransaction(it->it.copyToRealmOrUpdate(data));
realm.close();

Is it ok? To handle realm instance when I get an error I've added a while loop that close every instance of realm if an error occur

Realm realm = Realm.getDefaultInstance(); // that operation shouldn't cost too much AFAIK
/* An error occurred, so I delete from realm every record that might be corrupted and inform user that a reload is needed */
while(!realm.isClosed()) realm.close();

I haven't noticed issue with that at the moment, everything's working fine. Is there a better way to handle realm instance on a different thread?

try(Realm realm = Realm.getDefaultInstance()) {
   // use realm
} // auto-close

Thanks, everything's just working fine

There is some fix?
realm.close() is not working yet.
This issue should be reopened! @coletz

Currently I'm not having any problem, realm.close() is working as expected for me. I was having some threading problem (realm wasn't so good at that time, and I wasn't using it in the correct way). Normally I get the instance in the onCreate and close it in the onDestroy (or onStart and onStop with fragments). If realm is not closing, you probably did Realm.getDefaultInstance() more times than realm.close()

Realm is only closed if the ref count decrease of close() makes Realm.getLocalInstanceCount(config) == 0.

If you open Realm multiple times with getInstance, then you have to close it just as many times.

Was this page helpful?
0 / 5 - 0 ratings