Swift3 领域迁移:将对象从一个对象移动到另一个对象

Swift3 领域迁移:将对象从一个对象移动到另一个对象,swift3,realm,realm-migration,Swift3,Realm,Realm Migration,我有三个目标: class Customer: Object { dynamic var solution: Solution!; ... } class Solution: Object { dynamic var data: Data!; ... } class Data: Object { ... } 现在,我需要将数据对象从解决方案移动到客户,使其成为: class Customer: Object { dynamic var sol

我有三个目标:

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

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

class Data: Object {
    ...
}
现在,我需要将
数据
对象从
解决方案
移动到
客户
,使其成为:

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

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

我使用领域迁移示例应用程序进行了一些实验,并提出了以下潜在解决方案:

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

也就是说,可以嵌套调用
migration.enumerateObjects
,引用不同的领域模型对象类。因此,只需首先枚举
客户
对象,然后在每次迭代中,枚举
解决方案
对象,以找到具有正确
数据
值的对应对象。一旦找到,应该可以使用
解决方案
对象中的数据设置
客户
对象

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"]
                }
            }
        }
    }
})
Realm.Configuration.defaultConfiguration=Realm.Configuration(
阴谋厌恶:1,
migrationBlock:{migration,oldSchemaVersion in
if(oldschemaversation<1){
enumerateObjects(类型:Customer.className()){oldCustomerObject,newCustomerObject在中
enumerateObjects(类型:Solution.className()){oldSolutionObject,newSolutionObject在中
//检查解决方案对象是否为客户引用的对象
guard oldCustomerObject[“解决方案”]。isEqual(oldSolutionObject)else{return}
//复制数据
newCustomerObject[“数据”]=oldSolutionObject[“数据”]
}
}
}
}
})

我觉得我需要强调的是,这段代码绝对没有经过测试,也不能保证在当前状态下工作。因此,我建议您确保在事先不会错过的虚拟数据上对其进行彻底测试。:)

我用Realm migrations示例应用程序做了一些实验,并提出了这个潜在的解决方案:

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

也就是说,可以嵌套调用
migration.enumerateObjects
,引用不同的领域模型对象类。因此,只需首先枚举
客户
对象,然后在每次迭代中,枚举
解决方案
对象,以找到具有正确
数据
值的对应对象。一旦找到,应该可以使用
解决方案
对象中的数据设置
客户
对象

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"]
                }
            }
        }
    }
})
Realm.Configuration.defaultConfiguration=Realm.Configuration(
阴谋厌恶:1,
migrationBlock:{migration,oldSchemaVersion in
if(oldschemaversation<1){
enumerateObjects(类型:Customer.className()){oldCustomerObject,newCustomerObject在中
enumerateObjects(类型:Solution.className()){oldSolutionObject,newSolutionObject在中
//检查解决方案对象是否为客户引用的对象
guard oldCustomerObject[“解决方案”]。isEqual(oldSolutionObject)else{return}
//复制数据
newCustomerObject[“数据”]=oldSolutionObject[“数据”]
}
}
}
}
})
我觉得我需要强调的是,这段代码绝对没有经过测试,也不能保证在当前状态下工作。因此,我建议您确保在事先不会错过的虚拟数据上对其进行彻底测试。:)

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"]
                            }
                        }
                    }
                }
            }
        }
var config=Realm.Configuration()
config.migrationBlock={migration,oldSchemaVersion in
如果oldSchemaVersion<当前模式版本{
//枚举第一个对象类型
中的migration.enumerateObjects(类型:Message.className()){(oldMsg,newMsg)
//提取链接对象并从任意对象强制转换为DynamicObject
如果让msgAcct=oldMsg?[“帐户”]作为动态对象{
//枚举第二个对象类型
枚举对象(类型:Account.className()){(oldAcct,newAcct)在
如果让oldAcct=oldAcct{
//将提取的对象与枚举的对象进行比较
如果msgAcct.isEqual(oldAcct){
//成功!
newMsg?[“accountId”]=oldAcct[“accountId”]
}
}
}
}
}
}
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"]
                            }
                        }
                    }
                }
            }
        }
var config=Realm.Configuration()
config.migrationBlock={migration,oldSchemaVersion in
如果oldSchemaVersion<当前模式版本{
//枚举第一个对象类型
中的migration.enumerateObjects(类型:Message.className()){(oldMsg,newMsg)
//提取链接对象并从任意对象强制转换为DynamicObject
如果让msgAcct=oldMsg?[“帐户”]作为动态对象{