Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detach an Object from a Realm?

Tags:

ios

swift

realm

Lets say I have the following scenario...

ViewController1 loads a Person object from a Realm on the main thread and passes it to ViewController2. User interaction in ViewController2 causes the same Person object to change, but I only want to persist the changes once the User has pressed "Save".

Currently, when changing the passed Person object in ViewController2 a runtime error is thrown saying changes to an object need to be made in a Write block. This makes sense, but in this scenario I don't actually want to persist the changes right away.

  • Is there a way to detach an Object from a Realm to avoid these checks?
  • If there isn't, what would be a suggested work around? (Copying the Object to a new instance? Tracking the changes to the Object separately and applying them later? Both seem pretty messy.)
like image 539
lionpants Avatar asked Jul 29 '15 17:07

lionpants


1 Answers

Right now, you can make a 'standalone' copy of your object, via Object(value: existingObject) -- that'll probably be the simplest solution for now, until Realm adds something like nested transactions that will make undoing an arbitrary number of changes easier.

like image 173
segiddins Avatar answered Oct 21 '22 00:10

segiddins