Currently, I have two custom model classes. Between them previously I set up a one-to-one relationship. But now due to new requirements change, I need to convert the relationship into a one-to-many.
For example, this is how I previously structure my model classes
class Location: Object {
@objc dynamic private var id: String = NSUUID().uuidString
@objc dynamic var typeString: String =
}
class Job :Object {
@objc dynamic var id: String = NSUUID().uuidString
let location: Location?
}
新结构
class Job :Object {
@objc dynamic var id: String = NSUUID().uuidString
let locations = RealmSwift.List<Location>()
}
因此,我基本上想在职业类别中节省多个地点物体。 这一新结构对我来说是不起作用的。 但现在我想将旧数据移入这一新机构。 我试图利用以下法典实现这一目标,但没有任何成功。
migration.deleteData(forType: Location.className)
migration.enumerate(ofType:Job.className) { oldObject, newObject in
let location = migration.create(Location, value: oldObject!["location"]!)
(newObject!["locations"] as! List<MigrationObject>).append(location)
}