Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't close realm instance #3321

Closed
coletz opened this issue Aug 18, 2016 · 10 comments
Closed

Can't close realm instance #3321

coletz opened this issue Aug 18, 2016 · 10 comments
Labels

Comments

@coletz
Copy link

coletz commented Aug 18, 2016

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())

@Zhuinden
Copy link
Contributor

So where else do you open new Realm instances?

@coletz
Copy link
Author

coletz commented Aug 18, 2016

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

@Zhuinden
Copy link
Contributor

Zhuinden commented Aug 18, 2016

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

@kneth
Copy link
Member

kneth commented Aug 19, 2016

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

@coletz
Copy link
Author

coletz commented Aug 19, 2016

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?

@Zhuinden
Copy link
Contributor

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

@coletz
Copy link
Author

coletz commented Aug 19, 2016

Thanks, everything's just working fine

@tulioccalazans
Copy link

tulioccalazans commented Apr 26, 2018

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

@coletz
Copy link
Author

coletz commented Apr 26, 2018

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()

@Zhuinden
Copy link
Contributor

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

6 participants