I've added a new Model object to my realm objects and I am trying to move data from my old realm object properties to this new object.
In the old Schema, I have the following:
class Item: Object {
@objc dynamic var image = ""
@objc dynamic var coverImage = ""
@objc dynamic var video = ""
}
In the new schema, I've added a new property called media so now it's looking this this
class Item: Object {
@objc dynamic var image = ""
@objc dynamic var coverImage = ""
@objc dynamic var video = ""
@objc dynamic var media: Media?
}
I've also added this new Model object:
class Media: Object {
@objc dynamic var fullImage = ""
@objc dynamic var thumbnailImage = ""
@objc dynamic var video = ""
var item = LinkingObjects(fromType: Item.self, property: "media")
}
My goal is to move the data from the old Item objects to the new Media objects.
I was trying to do something like this, but I don't know how to migrate that linking object, any help in the right direction would be appreciated.
Realm.Configuration.defaultConfiguration = Realm.Configuration(
schemaVersion: 1,
migrationBlock: { migration, oldSchemaVersion in
if (oldSchemaVersion < 1) {
// enumerate first object
migration.enumerateObjects(ofType: Item.className()) { oldItem, newItem in
let image = oldItem?["image"] as! String
let coverImage = oldItem?["coverImage"] as! String
let video = oldItem?["video"] as! String
//migrate second object
migration.enumerateObjects(ofType: Media.className(), { (oldMedia, newMedia) in
})
}
}
})
You don't need to do anything with LinkingObjects
, realm calculates those automatically when you query them.
All you'll need to do in your migration is set media
to be a new Media
object with the values you already have.
Other notes:
enumerateObjects
isn't needed.image
, coverImage
, and video
from Item
since you're moving those value to Media
Edit: This is what you would need to have in your migration.
let media = Media()
media.fullImage = oldItem?["image"] as! String
media.thumbnailImage = oldItem?["coverImage"] as! String
media.video = oldItem?["video"] as! String
newItem?["media"] = media