In my project I am have LoginFragment(which accepts user name and password) PinFrgment which Accepts a PIN and Confirms that PIN and saves it in Realm. When the user confirms the PIN I am creating a realmconfiguration and getting the realm instance as shown below.
protected void initRealm(Context context, byte[] key) {
try {
Realm.init(context);
RealmConfiguration config = new RealmConfiguration.Builder().deleteRealmIfMigrationNeeded().encryptionKey(key).build();
Realm.setDefaultConfiguration(config);
realmInstance = Realm.getDefaultInstance();
DoPayApplication.getInstance().setRealmException(false);
} catch (RealmFileException realmFileException) {
DoPayApplication.getInstance().setRealmException(true);
Log.d(TAG, realmFileException.toString());
}catch (Exception ex){
Log.i(TAG, ex.toString());
DoPayApplication.getInstance().setRealmException(true);
}
}
On logout and forgot PIN I am calling the following method to close/reset realm.
public static void closeRealm(Context context) {
try{
if (realmInstance != null && !realmInstance.isClosed()) {
realmInstance.close();
RealmConfiguration config = realmInstance.getConfiguration();
realmInstance.deleteRealm(config);
}
}catch (Exception e){
Log.d(TAG, e.getMessage());
}
}
As long as I am in the app, on logout I am able to closeRealm as realmInstance is not null.
If the app is closed and user comes back to it. On forgot PIn when I call closeRealm 'realmInstance' is null and I cannot get the realmconfiguration to delete.
As the PIN is forgotten and the encryptionkey can not be generated. Is there a way to delete/recreate the real file without the Key.
Any advice would be very helpful.
Thanks R