Reputation: 75
I'm not able to change the class name of a Realm Object that has properties pointing to other Realm Objects. A class like this, for example.
class OldClass: Object {
var id: String!
var dog: Dog! //this is a Realm Object (with its own table)
}
I've seen the simple examples of how to do this.
migration.enumerateObjects(ofType: "OldClass", { (oldObject, newObject) in
migration.create("NewClass", value: oldObject!)
})
I expect the above would work if the schemas for both OldClass and NewClass were the same, AND if all the properties were non-Realm objects. If the schemas are different, I've gathered that you can do something like this.
migration.enumerateObjects(ofType: "OldClass", { (oldObject, newObject) in
let obj = migration.create("NewClass")
obj["id"] = (oldObject["id"] as! String)
obj["newPropertyName"] = (oldObject!["oldPropertyName"] as! Int)
})
Neither of these example seem to work when your object has a property pointing to another Realm object though. At least this is what I suspect, since I get RLMException 'Can't create object with existing primary key value'.
My suspicion is that the 'existing primary key' is referring to the Dog object, and that in migrating from NewClass to OldClass, the migration is trying to re-create the Dog object (which already exists).
How do I properly perform this type of migration?
Upvotes: 1
Views: 1553
Reputation: 7340
Unfortunately this feature is not implemented, we track it in https://github.com/realm/realm-cocoa/issues/2162. You can also find some useful info at https://github.com/realm/realm-cocoa/issues/4366.
Upvotes: 0