1

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

BRDroid
  • 3,920
  • 8
  • 65
  • 143

1 Answers1

0

You can just delete .realm file with native tools:

// realm file default name is default.ream
File file = new File(context.getFilesDir(), "realmFileName.realm");
file.delete();

In that way you can copy db dump from assets or raw resources, also without realm configurations:

//Load dump from assets folder
InputStream inputStream = application.getAssets().open("my_factory_data_db.realm");
// realm file default name is default.ream
File file = new File(context.getFilesDir(), "realmFileName.realm");
FileOutputStream outputStream = new FileOutputStream(file);
byte[] buf = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buf)) > 0) {
    outputStream.write(buf, 0, bytesRead);
}
ApiPreferenceHelper.setVersionApi(application, BuildConfig.BUILD_TIMESTAMP);
inputStream.close();
outputStream.close();

You can lear more about realm db file here How to find my realm file?

Link182
  • 733
  • 6
  • 15