【问题标题】:Realm Migration - Changing Name of Class (for complex class)领域迁移 - 更改类的名称(对于复杂类)
【发布时间】: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 对象(已经存在)。

如何正确执行此类迁移?

【问题讨论】:

    标签: swift realm


    【解决方案1】:

    很遗憾,这个功能没有实现,我们在https://github.com/realm/realm-cocoa/issues/2162 跟踪它。你也可以在https://github.com/realm/realm-cocoa/issues/4366找到一些有用的信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多