【问题标题】:Android : Realm Migration IssueAndroid:领域迁移问题
【发布时间】:2017-02-10 09:40:44
【问题描述】:

我是 android 新手,我在我的 APP 中使用了领域,但当时我没有指定 schemaversion,现在我想升级应用程序并实施迁移。

以前,我是这样做的,

Realm.init(getApplicationContext());

RealmConfiguration realmConfiguration =
                        new RealmConfiguration.Builder().build();

realm = Realm.getInstance(realmConfiguration);

现在,

Realm.init(getApplicationContext());

RealmConfiguration realmConfiguration =
                            new RealmConfiguration.Builder().schemaVersion(1).migration(new Migration()).build();

realm = Realm.getInstance(realmConfiguration);  

而且,我收到了这个错误。

java.lang.IllegalArgumentException: Configurations cannot be different if used to open the same file. 
Cached configuration: 
realmDirectory: /data/user/0/in.avanti_app.student_companion.debug/files
realmFileName : default.realm
canonicalPath: /data/data/in.avanti_app.student_companion.debug/files/default.realm
key: [length: 0]
schemaVersion: 1
migration: in.avanti_app.student_companion.realmClasses.Migration@25
deleteRealmIfMigrationNeeded: false
durability: FULL
schemaMediator: io.realm.DefaultRealmModuleMediator@793ec9a4

New configuration: 
realmDirectory: /data/user/0/in.avanti_app.student_companion.debug/files
realmFileName : default.realm
canonicalPath: /data/data/in.avanti_app.student_companion.debug/files/default.realm
key: [length: 0]
schemaVersion: 0
migration: null
deleteRealmIfMigrationNeeded: false
durability: FULL
schemaMediator: io.realm.DefaultRealmModuleMediator@793ec9a4

我还覆盖了 Migration 类中的 hasCode 和 equals。

【问题讨论】:

  • I also override the hasCode and equals in Migration class.请出示相关代码
  • 虽然它似乎声称你有schemaVersion1schemaVersion0的配置。
  • @EpicPandaForce public class Migration implements RealmMigration { Overirde public int hashCode() { return Migration.class.hashCode(); } Overirde public boolean equals(Object o) { return (o instanceof Migration); } Overirde public void migrate(final DynamicRealm realm, long oldVersion, long newVersion) { //迁移逻辑 } }
  • 您在代码中的某处创建了一个新的 RealmConfiguration,它没有指定新的架构版本和迁移。
  • 不,我已经检查了那部分。

标签: android realm realm-migration


【解决方案1】:

调用Realm.init(Context)会在下面的代码中初始化一个“默认配置”

public static synchronized void init(Context context) {
    if (BaseRealm.applicationContext == null) {
        if (context == null) {
            throw new IllegalArgumentException("Non-null context required.");
        }
        RealmCore.loadLibrary(context);
        defaultConfiguration = new RealmConfiguration.Builder(context).build();
        ObjectServerFacade.getSyncFacadeIfPossible().init(context);
        BaseRealm.applicationContext = context.getApplicationContext();
        SharedRealm.initialize(new File(context.getFilesDir(), ".realm.temp"));
    }
}

但是new RealmConfiguration.Builder(context).build(); 设置了没有架构版本、迁移、deleteIfMigrationNeeded() 等的配置

为了设置一个新的默认配置,你需要创建你的配置然后调用Realm.setDefaultConfiguration(realmConfig);


RealmConfiguration realmConfiguration =
         new RealmConfiguration.Builder().schemaVersion(1).migration(new Migration()).build();
Realm.setDefaultConfiguration(realmConfiguration);
realm = Realm.getDefaultInstance();  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多