I’m trying to update a property from a object with this code: object.property = 'abc';
But i’m getting this error: Error: RealmException: Error code: 2005 . Message: Trying to modify database while in read transaction)
I know if i use the below code it will work: realm!.write(() { object.property = 'abc' });
But the problem is that i dont want it to be synced with the database at this moment, i’ll sync it with the database later when/if the user clicks on the save button.
Is there any way to copy this object without the realm reference or detach it from realm?
This way i would be able to do that, or do you guys have any other sugestion?
Hi,
You could use async transactions to achieve what you describe. You can open an async transaction, allow each property of this object to be changed and commit the transaction on Save or revert the transaction on cancel.
Update: I mean you can use beginWrite/Transaction.commit APIs
We run into this all the time where the user has the ability to edit components of an object and then it should only be written out when the Save button is clicked.
There are two strategies that we use
Separate the UI from the Model - e.g. pass in the object to the view (sheet/window/etc) and populate the UI from it, allowing the user to edit at will.
• Once the user clicks Save then within a write transaction, update the properties on the object from the UI
Upon opening the view (sheet/window/etc) make a copy of the object and update the object copy as the user makes changes.
• Once the user clicks Save, then within a write transaction, write out the object via it’s primary key and the fields will be updated accordingly.