Problem updating Realm objects

I have two model classes Realm

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 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 = ... // 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 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, . , , . - , , ?

+4
1

, . , :

import UIKit
import RealmSwift

// Dog model
class Dog: Object {
    dynamic var name = ""
    dynamic var age = 0
    dynamic var owner: Person? // Properties can be optional

    override class func primaryKey() -> String? { return "name" }
}

// Person model
class Person: Object {
    dynamic var name = ""
    let dogs = List<Dog>()

    override class func primaryKey() -> String? { return "name" }
}

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.

        do {
            try NSFileManager.defaultManager().removeItemAtPath(Realm.Configuration.defaultConfiguration.path!)
        } catch {}

        let dogRexJSON: AnyObject = ["name": "Rex", "age" : 20]
        let dogLuckyJSON: AnyObject = ["name": "Lucky", "age" : 25]
        var somePerson = Person(value: ["name" : "Shurik", "dogs" : [dogRexJSON]])


        // Realms are used to group data together
        let realm = try! Realm() // Create realm pointing to default file

        // Save your object
        realm.beginWrite()
        realm.add(somePerson)
        try! realm.commitWrite()

        somePerson = Person(value: ["name" : "Shurik", "dogs" : [dogRexJSON, dogLuckyJSON]])
        try! realm.write { () -> Void in
            realm.add([somePerson], update: true)
            return
        }

        let val = realm.objectForPrimaryKey(Dog.self, key: "Lucky")
        print(val!.name) // as expected log >> Lucky

        return true
    }
}

, .

+3

Source: https://habr.com/ru/post/1626029/


All Articles