我在Realm.init
之后进行了下一个领域迁移。这似乎还可以。但是,当我到达使用领域的第一个函数时,会出现以下错误:
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: uk.Migrations
现在,我不确定这是怎么引起的,或者为什么,代码很简单,因此不应该引起该问题,但是,很明显,我在这里做错了什么?
这似乎仅在更新后发生。
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Initialize Realm (just once per application)
Realm.init(this);
RealmConfiguration config = new RealmConfiguration.Builder()
.schemaVersion(2) // Must be bumped when the schema changes
.migration(new Migrations()) // Migration to run instead of throwing an exception
.build();
Realm.setDefaultConfiguration(config);
}
Migrations.java
public class Migrations implements RealmMigration{
@Override
public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
// DynamicRealm exposes an editable schema
RealmSchema schema = realm.getSchema();
Log.v("Realm", String.valueOf(oldVersion));
if(oldVersion == 0){
schema.get("Shipment")
.addField("scannedOut", Boolean.class);
oldVersion++;
}
if(oldVersion == 1){
schema.get("Shipment")
.addField("scannedIn", Boolean.class);
oldVersion++;
}
}
}
使用第一名Realm,该错误由第一行触发。在迁移运行之后,确实会发生这种情况。
Realm realm = Realm.getDefaultInstance();
realm.beginTransaction();
realm.copyToRealmOrUpdate(technician);
realm.commitTransaction();
答案 0 :(得分:0)
您听起来好像多次创建相同的RealmConfiguration(就像在Activity中而不是在Application中初始化Realm一样),但是RealmConfigurations彼此不相等,因为您没有在提供的配置中实现equals / hashCode课程。
public class Migrations implements RealmMigration{
....
@Override public boolean equals(Object obj) {
return obj != null && obj instanceof Migrations;
}
@Override public int hashCode() {
return Migrations.class.hashCode();
}
}