领域迁移:将对象迁移到列表中

Realm Migration: Migrate an object into a List

所以,我一直在尝试迁移我的 Realm 架构,但我似乎无法执行以下操作。

oldSchema 中,我有以下内容:

class Period: Object {
  dynamic var weekday: Weekday! // this is just another Realm Object
}

newSchema 中,我正在尝试将工作日移至工作日列表中。

class Period: Object {
  let weekdays: List<Weekday> = List<Weekday>()
}

执行 Realm 迁移时,如何将 weekday 对象从 oldSchema 移动到 newSchema 中的 weekdays 列表。

谢谢。

您可以在 Realm 配置下 运行 迁移块。

Realm.Configuration.defaultConfiguration = Realm.Configuration(
schemaVersion: 2,
migrationBlock: { migration, oldSchemaVersion in

    migration.enumerate(Period.className()) { oldObject, newObject in
        // filter the versions where this change would take place
        // if oldSchemaVersion < 1 {
        // }...
        newObject["weekdays"] = [oldObject["weekday"]]              
   })