I'm using Realm for Android to store some data. When the user presses the log out button, I'd like to clear my entire Realm database. To do that, I have the following snippet of code:
realm.close();
realm.deleteRealmFile(this);
goToLoginActivity();
The issue now is in my onResume function. I'm getting the following exception:
Caused by: java.lang.IllegalStateException: This Realm instance has already been closed, making it unusable.
My onResume code is as follows:
@Override
protected void onResume() {
super.onResume();
// I'm trying to check if the realm is closed; this does not work
if (realm == null) {
realm = Realm.getInstance(this);
}
// Do query using realm
}
How can I check to see if a realm object has been closed already? Alternatively, is there a better way to clear the database than deleting the realm file?
--
Edit: Just saw How can I easily delete all objects in a Realm for iOS. Any word on when the deleteAllObjects API will be available for Android? At the time of writing, Android is at version 0.80.0 and the API is available in iOS in 0.87.0.