android-Realm无法迁移

我有一个要应用迁移的Realm模型.但是,当我应用迁移时,我得到了错误

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: 

在我的Activity类中,配置设置为:

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

我使用领域实例来获取一些值.然后我使用以下方法应用迁移:

RealmConfiguration config = new RealmConfiguration.Builder(this)
            .schemaVersion(1) // Must be bumped when the schema changes
            .migration(new Migration()) // Migration to run
            .build();

Realm.setDefaultConfiguration(config);

当我这样称呼时:realm = Realm.getDefaultInstance();我收到上面的错误.我是否正确应用了迁移?

解决方法:

您的迁移应如下所示:

public class MyMigration implements Migration {
    //... migration

    public int hashCode() {
       return MyMigration.class.hashCode();
    }

    public boolean equals(Object object) {
       if(object == null) { 
           return false; 
       }
       return object instanceof MyMigration;
    }
}
上一篇:android-在RealmMigration中创建对象并将其添加到RealmList


下一篇:java-更深入地了解Realm的工作原理?