realmrealm-java

realm java force creating of new realm instance


If you create new RealmConfiguration and then call Realm.getInstance(configuration)

it will use the internal cache. If i delete the realm file, then create new instance of configuration calling of Realm.getInstance(configuration) won't create new Realm instance and realm file (or reload it) again. I need to force Realm to do so and skip or clean the internal cache.


Solution

  • Calling realm.close(); seems to does the job. But you have to make sure you capture only one local Realm variable or one close() call won't work.

    Bad example:

    public void importRealmFile(Context context) {
        Realm realm = contexts.getImportContext().getRealm();
        Storages importStorages = new Storages(contexts.getImportContext().getRealm());
        realm.close(); // doesn't free the realm from the cache
    

    Good example:

    public void importRealmFile(Context context) {
        Realm realm = contexts.getImportContext().getRealm();
        Storages importStorages = new Storages(realm); 
        realm.close(); // works good