领域迁移 - 将新的主键初始化为 int
Realm Migration - initialize new primary key as int
在我们的应用程序中,我们向其中一个元素添加了一个新的主键(实际上,这是很久以前的事了)。所以很自然地,需要迁移。问题是,测试几乎是不可能的,因为没有人真正知道,首先是如何生成这些对象(而且无论出于何种原因,intellij 也没有提供任何答案)
无论如何,这是我的迁移代码:
public class CustomMigration implements RealmMigration{
private int currentKey = 0;
public void migrate(DynamicRealm realm, long oldVersion, long newVersion){
RealmSchema schema = realm.getSchema();
if(oldVersion <= 4){}
if(schema.contains("AvailableCandidate"){
if(!schema.get("AvailableCandidate").hasField("pos")){
.addField("pos", int.class, FieldAttribute.PRIMARY_KEY)
.transform(new RealmObjectSchema.Function() {
@Override
public void apply(DynamicRealmObject obj) {
obj.setInt("pos", currentKey++);
}
});
}
}
//
// here be more code
//
oldVersion = 5;
}
}
}
特别注意变量currentKey
。我认为 transform 会像迭代器一样工作,并且 currentKey 应该在每次 transform 迭代时递增。
问题是,仍然有用户似乎遇到了那个错误,而且 currentKey 似乎没有增加。
这个棘手问题的解决方案是什么?
编辑:fabric 吐出的异常如下:
"pos" cannot be a primary key, it already contains duplicate values: 0
您应该只在字段内的值不违反约束时添加主键约束。
public class CustomMigration implements RealmMigration{
private int currentKey = 0;
public void migrate(DynamicRealm realm, long oldVersion, long newVersion){
RealmSchema schema = realm.getSchema();
if(oldVersion <= 4){}
if(schema.contains("AvailableCandidate"){
if(!schema.get("AvailableCandidate").hasField("pos")){
.addField("pos", int.class, FieldAttribute.INDEXED)
.transform(new RealmObjectSchema.Function() {
@Override
public void apply(DynamicRealmObject obj) {
obj.setInt("pos", currentKey++);
}
})
.addPrimaryKey("pos");
}
}
//
// here be more code
//
oldVersion = 5;
}
}
@Override
public boolean equals(Object obj) {
if(obj == null) {
return false;
}
return CustomMigration.class.equals(obj.getClass());
}
@Override
public int hashCode() {
return CustomMigration.class.hashCode();
}
}
在我们的应用程序中,我们向其中一个元素添加了一个新的主键(实际上,这是很久以前的事了)。所以很自然地,需要迁移。问题是,测试几乎是不可能的,因为没有人真正知道,首先是如何生成这些对象(而且无论出于何种原因,intellij 也没有提供任何答案)
无论如何,这是我的迁移代码:
public class CustomMigration implements RealmMigration{
private int currentKey = 0;
public void migrate(DynamicRealm realm, long oldVersion, long newVersion){
RealmSchema schema = realm.getSchema();
if(oldVersion <= 4){}
if(schema.contains("AvailableCandidate"){
if(!schema.get("AvailableCandidate").hasField("pos")){
.addField("pos", int.class, FieldAttribute.PRIMARY_KEY)
.transform(new RealmObjectSchema.Function() {
@Override
public void apply(DynamicRealmObject obj) {
obj.setInt("pos", currentKey++);
}
});
}
}
//
// here be more code
//
oldVersion = 5;
}
}
}
特别注意变量currentKey
。我认为 transform 会像迭代器一样工作,并且 currentKey 应该在每次 transform 迭代时递增。
问题是,仍然有用户似乎遇到了那个错误,而且 currentKey 似乎没有增加。
这个棘手问题的解决方案是什么?
编辑:fabric 吐出的异常如下:
"pos" cannot be a primary key, it already contains duplicate values: 0
您应该只在字段内的值不违反约束时添加主键约束。
public class CustomMigration implements RealmMigration{
private int currentKey = 0;
public void migrate(DynamicRealm realm, long oldVersion, long newVersion){
RealmSchema schema = realm.getSchema();
if(oldVersion <= 4){}
if(schema.contains("AvailableCandidate"){
if(!schema.get("AvailableCandidate").hasField("pos")){
.addField("pos", int.class, FieldAttribute.INDEXED)
.transform(new RealmObjectSchema.Function() {
@Override
public void apply(DynamicRealmObject obj) {
obj.setInt("pos", currentKey++);
}
})
.addPrimaryKey("pos");
}
}
//
// here be more code
//
oldVersion = 5;
}
}
@Override
public boolean equals(Object obj) {
if(obj == null) {
return false;
}
return CustomMigration.class.equals(obj.getClass());
}
@Override
public int hashCode() {
return CustomMigration.class.hashCode();
}
}