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?
}
New structure
class Job :Object {
@objc dynamic var id: String = NSUUID().uuidString
let locations = RealmSwift.List<Location>()
}
So basically I want to save multiple location objects inside Job class. This new structure is working fine for me. But now I want to migrate old data into this new setup. I try to achieve this using the following code but no success.
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)
}