Hi,--I am scratching my head and not sure what I am doing wrong here so advice would be really appreciated. I have an object with a primary key :
class TheObject: Object {
dynamic var id = 0 //primary key
override static func primaryKey() -> String?{//return primary key
return "id"
}
I want to create or update the object :func createOrUpdate(){
let object = TheObject()
if let key = json["id"] as? Int { object.id = key }
let realm = try! Realm()
try realm.write{realm.add(object, update:true)
}
}After reading Realm Swift documentation I believe that realm.add(object, update:true) should update the object if it already exist but I get the error *** Terminating app due to uncaught exception 'RLMException', reason: 'Can't set primary key property 'key' to existing value '123'.'Thanks!
You received this message because you are subscribed to the Google Groups "Realm" group.
To unsubscribe from this group and stop receiving emails from it, send an email to realm-cocoa...@googlegroups.com.
To post to this group, send email to realm...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/realm-cocoa/59eb3cb8-6b5d-4f39-9bbc-b84675c99b8a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.