我更新到最新版本希望它能解决我的问题,但它没有。我正在使用
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
当只是尝试独立运行它甚至不从以前的版本更新时。真的不知道该怎么做了。真的需要它来工作,所以我不必删除每个人的数据。考虑制作更正式的错误报告,但想先检查是否有其他人知道是否有解决方案。每当我尝试获取默认配置时都会出现此问题。它通常在我第一次打开应用程序时有效,但在我进入下一个 Activity 时崩溃
最佳答案
我会说错误消息非常具体,它说你应该执行以下操作
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();
}
}
关于android - 什么都不起作用。 Realm 迁移 [Android 1.2],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39116272/