什么都行不通。领域迁移[Android 1.2]

时间:2016-08-24 06:50:26

标签: android realm realm-migration

我更新到最新版本,希望它能解决我的问题,但事实并非如此。我正在使用

  RealmConfiguration config = new RealmConfiguration.Builder(this)
            .name("myrealm.realm")
           .migration(new Migration())
            .schemaVersion(2) // 2
            .build();
    try {
        Realm realm = Realm.getInstance(config); // Automatically run migration if needed
        realm.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    Realm.setDefaultConfiguration(config);

此代码用于更新和添加一些新对象。这是我的迁移

public class Migration implements RealmMigration {
@Override
public void migrate(final DynamicRealm realm, long oldVersion, long newVersion) {
    // Access the Realm schema in order to create, modify or delete classes and their fields.
    RealmSchema schema = realm.getSchema();
    // Migrate from version 1 to version 2
    if (oldVersion == 1) {
        // Create a new classes
        RealmObjectSchema styleSchema = schema.create("SavedStyle").addField("title", String.class).addField("json", String.class);
        RealmObjectSchema dictSchema = schema.create("SavedDictionary").addField("title", String.class).addField("dictionary", String.class);
        RealmObjectSchema journalSchema = schema.create("CustomJournal").addField("title", String.class).addField("json", String.class);
        oldVersion++;
    }
    if (oldVersion < newVersion) {
        throw new IllegalStateException(String.format("Migration missing from v%d to v%d", oldVersion, newVersion));
    }
}

}

我收到错误

java.lang.IllegalArgumentException: Configurations cannot be different if used to open the same file. The most likely cause is that equals() and hashCode() are not overridden in the migration class: otherClasses.Migration

当试图独立运行它时,甚至没有从以前的版本更新。不太确定该怎么做。真的需要它才能工作,所以我不必擦除每个人的数据。考虑制作更正式的错误报告,但想检查是否有其他人知道是否有解决方案。每当我尝试获取默认配置时,就会出现此问题。它通常在我第一次打开应用程序时起作用,但在我进入下一个活动时崩溃

1 个答案:

答案 0 :(得分:1)

我说错误信息非常具体,它说你应该执行以下操作

public class Migration implements RealmMigration {
    @Override
    public void migrate(final DynamicRealm realm, long oldVersion, long newVersion) {
        //...
    }

    @Override
    public boolean equals(Object object) {
         return object != null && object instanceof Migration;
    }

    @Override
    public int hashCode() {
         return Migration.class.hashCode();
    }
}