【发布时间】:2017-03-06 20:39:24
【问题描述】:
我无法更改具有指向其他领域对象的属性的领域对象的类名。比如这样的一个类。
class OldClass: Object {
var id: String!
var dog: Dog! //this is a Realm Object (with its own table)
}
我已经看到了如何做到这一点的简单示例。
migration.enumerateObjects(ofType: "OldClass", { (oldObject, newObject) in
migration.create("NewClass", value: oldObject!)
})
如果 OldClass 和 NewClass 的模式相同,并且所有属性都是非领域对象,我希望上述方法会起作用。如果架构不同,我收集到您可以这样做。
migration.enumerateObjects(ofType: "OldClass", { (oldObject, newObject) in
let obj = migration.create("NewClass")
obj["id"] = (oldObject["id"] as! String)
obj["newPropertyName"] = (oldObject!["oldPropertyName"] as! Int)
})
当您的对象具有指向另一个 Realm 对象的属性时,这些示例似乎都不起作用。至少这是我怀疑的,因为我得到 RLMException 'Can't create object with existing primary key value'。
我怀疑“现有主键”是指 Dog 对象,并且在从 NewClass 迁移到 OldClass 时,迁移正在尝试重新创建 Dog 对象(已经存在)。
如何正确执行此类迁移?
【问题讨论】: