Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to delete Android Realm file, but getting exception

Tags:

android

realm

I am trying to delete the Realm file to clear cache when a user logs out.

Here is what I'm trying:

Realm realm = Realm.getInstance(activity);

realm.close();
RealmConfiguration config = realm.getConfiguration();

Realm.deleteRealm(config);

I get this exception:

11-05 23:56:00.902 27521-27521/com.my.app E/AndroidRuntime: java.lang.IllegalStateException: It's not allowed to delete the file associated with an open Realm. Remember to close() all the instances of the Realm before deleting its file.
11-05 23:56:00.902 27521-27521/com.my.app E/AndroidRuntime:     at io.realm.Realm.deleteRealm(Realm.java:1726)

I'm obviously closing something, but its saying more are open? How do I close all of the realms so I can blow everything away and start fresh?

like image 637
tyler Avatar asked Oct 24 '25 05:10

tyler


1 Answers

Quoting from Realm documentation:

Realm implements Closeable in order to take care of native memory deallocation and file descriptors so it is important to remember to close your Realm instances when you are done with them.

Realm instances are reference counted, which means that if you call getInstance() twice in a thread, you will also have to call close() twice as well.

Probably you are missing to call close() on every Realm instance you created.

Check your code to find where getInstance() hasn't be matched by a close().

like image 196
Mattia Maestrini Avatar answered Oct 26 '25 19:10

Mattia Maestrini



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!