android - 什么都不起作用。 Realm 迁移 [Android 1.2]

标签 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

当只是尝试独立运行它甚至不从以前的版本更新时。真的不知道该怎么做了。真的需要它来工作,所以我不必删除每个人的数据。考虑制作更正式的错误报告,但想先检查是否有其他人知道是否有解决方案。每当我尝试获取默认配置时都会出现此问题。它通常在我第一次打开应用程序时有效,但在我进入下一个 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/

相关文章:

android - Realm 数据库是否可移植

android - MediaController上的“播放/暂停”按钮不会单独刷新音频播放器

swift - 移除 Realm 中的列

java - 非法状态异常 : It's not allowed to delete the file associated with an open Realm

swift - Realm LazyFilterBidirectionalCollection 错误 Swift 3

Android Realm : Primary key constraint broken. 值已存在:0

ios - Realm 迁移引入主键

android - 由于等级错误,无法在android studio中构建APK错误:任务执行失败

Android Google Mapview Activity 未在 Android Studio 中打开

android - 如何为 RenderScript 输出分配 bool 数组?