I have two model classes Realm
class ModelA: Object {
let id = RealmOptional<Int>()
dynamic var name: String!
}
class ModelB: Object {
let id = RealmOptional<Int>()
let models = List<ModelA>()
}
And I have an object JSONthat contains data for these models. I create an instance ModelBand then populate it with a list of instances ModelAas follows:
let json: JSON = ...
let myModelB = ModelB()
myModelB.id.value = json["id"].object as? Int
let modelsA = json["models"].map { ModelA(value: $0.1.object) }
myModelB.models.appendContentsOf(modelsA)
The reason I use different approaches is because the property names in JSONdo not match my property names for ModelB, but for ModelAthis is normal. Somewhere later I use realm.add(objects, update: true)(internally realm.write) and this leads to the following exception:
Application termination due to an undetected exception "NSUnknownKeyException", reason: "[valueForUndefinedKey:]: this class is not a key value compatible with the code for the key (null)".
:
, Realm(). add (_: update:).
ModelA ModelB primaryKey(), , , .
, update realm.deleteAll() ( write ). :
- "RLMException", : " " id " " xxxxxxx ".
, , Xcode. , Realm . Realm Xcode, . , , . - , , ?