1

我更新到最新版本希望它能解决我的问题,但它没有。我在用

  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

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

4

1 回答 1

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();
    }
}
于 2016-08-24T08:08:11.850 回答