领域在第一次通话时崩溃。如果用于打开同一文件,配置不能有所不同

时间:2016-03-21 09:00:11

标签: android database configuration realm realm-migration

我收到此错误:

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

下面:

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

在这一行:

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

我的配置是这样完成的:

  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);
        }

我的问题是,如何关闭/关闭打开文件的配置,以便用我的第二个配置打开它(带有deleteRealmIfMigrationNeeded的配置?

1 个答案:

答案 0 :(得分:3)

我不确定您要使用上面的代码尝试实现什么,但是您将两种不同的配置关联到同一个Realm文件中:

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

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

这违反了合同,正如错误消息所阐明的那样。