realm-java icon indicating copy to clipboard operation
realm-java copied to clipboard

Realm.deleteRealmFile() doesn't return the correct result

Open dotgc opened this issue 11 years ago • 5 comments

I have a user session based app. On logout I call Realm.deleteRealmFile(context) and it returns true. But when I log in as another user, the data of the previous Realm still exists. How to remove all data ?

I'm on Android and using Realm 0.76 for Android.

dotgc avatar Jan 05 '15 08:01 dotgc

Hi @ImGauravC If you call deleteRealmFile() while the Realm is still open the file will not be deleted, ie. you need to call Realm.close() on all your open instances.

Alternatively instead of deleting the Realm file you can call Realm.clear(Session.class) which will delete all objects of type Session while keeping the Realm file.

Does that answer your question?

cmelchior avatar Jan 05 '15 08:01 cmelchior

Thanks. That worked. But deleteRealmFile does return true. Shouldn't it return false if the file is not deleted in such a case?

dotgc avatar Jan 05 '15 22:01 dotgc

Hi @ImGauravC

We rely on the return value of Java's File.delete() which returns true and it reports the wrong result due to how the file system works.

It is documented in the JavaDoc for this reason as well, but we could probably add some additional checks that could catch some more edge cases. However for some use cases it will probably never be possible, like eg. two processes having the file open.

cmelchior avatar Jan 07 '15 05:01 cmelchior

I see. Thanks for the response @cmelchior :)

dotgc avatar Jan 09 '15 18:01 dotgc

We have added a check to Realm.deleteRealmFile() that will throw an error if called incorrectly. https://github.com/realm/realm-java/pull/787

It will be released as part of 0.79.0.

cmelchior avatar Feb 12 '15 20:02 cmelchior