Search code examples
androiddatabaseconfigurationrealmrealm-migration

Realm crashes at first call. Configurations cannot be different if used to open the same file


I get this error:

Fatal Exception: java.lang.IllegalArgumentException
Configurations cannot be different if used to open the same file. Cached configuration: realmFolder: /data/user/0/nl.hgrams.passenger/files realmFileName : myrealm.realm canonicalPath: /data/data/nl.hgrams.passenger/files/myrealm.realm key: [length: 0] schemaVersion: 3 migration: null deleteRealmIfMigrationNeeded: false durability: FULL schemaMediator: io.realm.DefaultRealmModuleMediator@ea820c5a New configuration: realmFolder: /data/user/0/nl.hgrams.passenger/files realmFileName : myrealm.realm canonicalPath: /data/data/nl.hgrams.passenger/files/myrealm.realm key: [length: 0] schemaVersion: 3 migration: null deleteRealmIfMigrationNeeded: true durability: FULL schemaMediator: io.realm.DefaultRealmModuleMediator@ea820c5a

Here:

 io.realm.RealmCache.validateConfiguration (RealmCache.java:226)
io.realm.RealmCache.createRealmOrGetFromCache (RealmCache.java:103)
io.realm.Realm.getInstance (Realm.java:197)

At this line:

Realm realm = Realm.getInstance(PSLocationCenter.getInstance().Config);

My config is done like this:

  Realm realm = null, realmActive = null;
        try {
            // should throw as migration is required
            Log.i("", "migration entered here, tried to open");
            if (Config == null) {
                Config = new RealmConfiguration.Builder(PSLocationCenter.this).name("myrealm.realm").schemaVersion(3).build();
            }
            if (activeConfig == null) {
                activeConfig = new RealmConfiguration.Builder(PSLocationCenter.this).name("active.realm").schemaVersion(2).build();
            }
            realm = Realm.getInstance(Config);
            realmActive = Realm.getInstance(activeConfig);
        } catch (RealmMigrationNeededException ex) {
            if(realm != null)
                realm.close();
            if(realmActive != null)
                realmActive.close();
            Config = new RealmConfiguration.Builder(PSLocationCenter.this).name("myrealm.realm").schemaVersion(3).deleteRealmIfMigrationNeeded().build();
            activeConfig = new RealmConfiguration.Builder(PSLocationCenter.this).name("active.realm").schemaVersion(2).deleteRealmIfMigrationNeeded().build();
            PSLocationCenter.getInstance().pref.setAuthenticationToken(null);
        }

My question is, how can I close/dismiss the configuration that opens the file, so that I open it with my second configuration (the one with deleteRealmIfMigrationNeeded?


Solution

  • I'm not sure what you are trying to achieve with the code above, but you are associating two different configurations to the same Realm file:

    line 5: Config = new RealmConfiguration.Builder(PSLocationCenter.this).name("myrealm.realm").schemaVersion(3).build();
    

    and

    line 17: Config = new RealmConfiguration.Builder(PSLocationCenter.this).name("myrealm.realm").schemaVersion(3).deleteRealmIfMigrationNeeded().build();
    

    This is against the contract as clarified by the error message.