realm-kotlin icon indicating copy to clipboard operation
realm-kotlin copied to clipboard

Improved error message when trying to update frozen object

Open rorbech opened this issue 2 years ago • 0 comments

It is quite easy to try to update frozen objects in a write block by:

val realm = Realm.open(...)
realm.write {
    val obj = realm.query<Obj>(...).find().first()
    obj.id = "newid"
}

The above will yield something like

[RLM_ERR_WRONG_TRANSACTION_STATE]: Must be in a write transaction

because the obj is obtained from the global realm instance and not the MutableRealm receiver of the write-block, like:

val realm = Realm.open(...)
realm.write { // this: MutableRealm
    val obj = query<Obj>(...).find().first() // implicit this
    obj.id = "newid"
}

It should be possible to catch this core-error and give a more precise error message hinting that user is updating a frozen object and maybe direct them towards using the mutable realm.

rorbech avatar Aug 03 '23 11:08 rorbech