我有一个领域模型,我想应用迁移。但是,当我应用迁移时,我会得到一个错误

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:

在我的活动类中,配置设置为:
realmConfiguration = new RealmConfiguration
                .Builder(this)
                .schemaVersion(0)
                .migration(new Migration())
                .build();

我使用realm实例来获取一些值。然后使用以下方法应用迁移:
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;
    }
}

07-28 02:49
查看更多