I have two Realm model classes
class ModelA: Object {
let id = RealmOptional<Int>()
dynamic var name: String!
// some other variables that are also String! type
}
class ModelB: Object {
let id = RealmOptional<Int>()
let models = List<ModelA>()
// other variables
}
And I have some JSON object which contains data for that models. I create ModelB instance then populate it with ModelA instances list the following way:
let json: JSON = ... // get it from somewhere, then use SwiftyJSON
let myModelB = ModelB()
myModelB.id.value = json["id"].object as? Int
// set other properties
let modelsA = json["models"].map { ModelA(value: $0.1.object) }
myModelB.models.appendContentsOf(modelsA)
The reason why I am using different approaches here is that propery names in JSON doesn't match to my property names for ModelB but for ModelA it is ok.
Somewhere later I use realm.add(objects, update: true) (inside realm.write) and that leads to the following exception:
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ valueForUndefinedKey:]: this class is not key value coding-compliant for the key (null).'
According to the documentation:
If your model class includes a primary key, you can have Realm intelligently update or add objects based off of their primary key values using Realm().add(_:update:).
so both ModelA and ModelB has primaryKey() function and I believe that should work but it is not.
Furthermore, I removed update parameter in the call and added call to the realm.deleteAll() before adding new objects (both in write callback). In that case I get the following exception:
Terminating app due to uncaught exception 'RLMException', reason: 'Can't set primary key property 'id' to existing value 'xxxxxxx'.
Additionally if I try to go through the call stack, Xcode crashes. It also crashes if I try to inspect any Realm object in the debugger. I already have installed Realm Xcode plugin but nothing changes. I cannot understand what is going wrong here and why I get so strange behavior. Could someone tell me where is my mistake please?