1

我收到此错误:

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 的配置?

4

1 回答 1

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

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

于 2016-03-21T10:30:48.613 回答