Given the following data model, is there a way to update or set a child relationship without updating the (existing) child object?
Data Model
class Person: Object {
dynamic var id = 0
dynamic var dog: Dog?
override static func primaryKey() -> String? {
return "id"
}
}
class Dog: Object {
dynamic var id = 0
dynamic var name = ""
override static func primaryKey() -> String? {
return "id"
}
}
Code
let realm = Realm()
// query existing dog
let existingDog = realm.objectForPrimaryKey(Dog.self, key: 42)
print(existingDog) // id: 42, name: Rex
// create new dog and person
let newDog = Dog(value: ["id": 42, "name": "Pluto"])
let person = Person()
realm.write {
// this shouldn't update the existing dog
// i.e. the name of the dog with id: 42 in the database should still be "Rex"
person.dog = newDog
realm.add(person, update: true)
}
I've also tried to do this via realm.create
but had no success either. Any ideas?