0

So the context is that I made a realm object and is giving one of it's variables a value, to do this I go ahead and call an instance of this object, then I connect to my server, get some value, then say something like

    let someObject = someObjectClass() //this being a realm object class

    someQuerySuccessBlock { (success, error) -> void in
        ...
        if let someValue = objects[0].value {
            someObject.id = someValue    //this line is where the issue is
        }
        ...
    })

    let realm = RLMRealm.defaultRealm()
    realm.beginWriteTransaction
    realm.addObject(someObject)
    realm.commitWriteTransaction

Error in llvm is error: execution was interrupted, reason: breakpoint 1.2. Error does not show unless i make a breakpoint for all exceptions.

Also to note that code does compile, does run, will not cause a crash (but simply.. not run any of the code from that specific line onwards. That someObject does get saved and created, but the field that is to be assigned simply did not get assigned, etc

Tonton
  • 68
  • 1
  • 7

2 Answers2

0

After some testing around, turns out this is because the realm object was already saved into Realm, where as the query block is async, it was trying to write into a variable of an object that had already been added.

Seems like the error was only like this because what I was trying to edit was the primaryKey of the object?

My fix:

    let someObject = someObjectClass() //this being a realm object class

    someQuerySuccessBlock { (success, error) -> void in
        ...
        if let someValue = objects[0].value {
            someObject.id = someValue    //this line is where the issue is

            let realm = RLMRealm.defaultRealm()
            realm.beginWriteTransaction
            realm.addObject(someObject)
            realm.commitWriteTransaction
        }
        ...
    })
Tonton
  • 68
  • 1
  • 7
0

If you try to edit the primary key of a saved object, then you will hit an assertion. Primary keys in Realm are immutable. Depending on your exact needs for your use case, you may want to create a new instance of your object class and assign all new properties which should be saved. You can add this new object then in a write transaction with -createOrUpdateInRealm:withValue:. Note: Be careful with to-one relations and other nullable properties as the merging strategy is here that null values are overwritten.

marius
  • 7,766
  • 18
  • 30