【问题标题】:Realm Migration: Move an Object from one Object to another Object领域迁移:将对象从一个对象移动到另一个对象
【发布时间】:2016-11-10 14:15:38
【问题描述】:

我有三个对象:

class Customer: Object {
    dynamic var solution: Solution!;
    ...
}

class Solution: Object {
    dynamic var data: Data!;
    ...
}

class Data: Object {
    ...
}

现在我需要将 Data 对象从 Solution 移动到 Customer 以便它变为:

class Customer: Object {
    dynamic var solution: Solution!;
    dynamic var data: Data!;
    ...
}

我不知道如何实现我的领域迁移方法,以便一切正常并且不会丢失数据。

【问题讨论】:

    标签: swift3 realm realm-migration


    【解决方案1】:

    我对 Realm 迁移示例应用做了一些实验,并想出了这个潜在的解决方案:

    在迁移块中,您只能通过 migration 对象与您的 Realm 文件进行交互。任何在迁移过程中直接访问 Realm 文件的尝试都会导致异常。

    话虽如此,对 migration.enumerateObjects 的嵌套调用可能会引用不同的 Realm 模型对象类。因此,它应该只是最初枚举Customer 对象的问题,并且在每次迭代中,枚举Solution 对象以找到具有正确data 值的对应对象。找到后,应该可以使用来自Solution 对象的数据设置Customer 对象。

    Realm.Configuration.defaultConfiguration = Realm.Configuration(
        schemaVersion: 1,
        migrationBlock: { migration, oldSchemaVersion in
            if (oldSchemaVersion < 1) {
                migration.enumerateObjects(ofType: Customer.className()) { oldCustomerObject, newCustomerObject in
                    migration.enumerateObjects(ofType: Solution.className()) { oldSolutionObject, newSolutionObject in
                        //Check that the solution object is the one referenced by the customer
                        guard oldCustomerObject["solution"].isEqual(oldSolutionObject) else { return }
                        //Copy the data
                        newCustomerObject["data"] = oldSolutionObject["data"]
                    }
                }
            }
        }
    })
    

    我觉得我需要强调的是,这段代码绝对没有经过测试,也没有保证在当前状态下可以正常工作。所以我建议你确保在一些你不会错过的虚拟数据上彻底测试它。 :)

    【讨论】:

      【解决方案2】:

      Swift 4,领域 3

      我必须迁移一个链接到另一个对象的领域对象。我想删除显式链接并将其替换为对象 ID。 TiM 的解决方案让我大部分都成功了,只需要一点改进。

         var config = Realm.Configuration()
         config.migrationBlock = { migration, oldSchemaVersion in
              if oldSchemaVersion < CURRENT_SCHEMA_VERSION {
      
                  // enumerate the first object type
                  migration.enumerateObjects(ofType: Message.className()) { (oldMsg, newMsg) in 
      
                      // extract the linked object and cast from Any to DynamicObject
                      if let msgAcct = oldMsg?["account"] as? DynamicObject {
      
                          // enumerate the 2nd object type
                          migration.enumerateObjects(ofType: Account.className()) { (oldAcct, newAcct) in
                              if let oldAcct = oldAcct {
      
                                   // compare the extracted object to the enumerated object
                                   if msgAcct.isEqual(oldAcct) {
      
                                      // success!
                                      newMsg?["accountId"] = oldAcct["accountId"]
                                  }
                              }
                          }
                      }
                  }
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-30
        • 1970-01-01
        • 2017-09-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多