I see 10.5.2 adds the ability to thaw a frozen object
Add support for “thawing” objects. Realm , Results , List and Object now have thaw() methods which return a live copy of the frozen object. This enables app behvaior where a frozen object can be made live again in order to mutate values. For example, first freezing an object passed into UI view, then thawing the object in the view to update values.
Maybe just a little clarification would help (me) on this
return a live copy
What part is a copy? How ‘live’ is it? What thread is it on - the same as the original?
If an object is frozen and then thawed, does that create a copy of the object which would be equivalent to
let unmanagedObject = MyObjectClass(value: someManagedObject)
I guess copy is a bit of a misnomer here. What it does is it simplifies the process of looking up an object in a live Realm. It’s a bit more convoluted under the hood, but think of it as using the identifier of the frozen realm/object/collection to look it up in the live Realm. It’s not copying any data and in reality, the live version may very well be different from the frozen one - e.g. if a transaction had been committed in the meantime.
A thawed object is just a regular managed Realm object, just resolved on the thread where it’s thawed. All rules for regular objects apply to it as well (i.e. you can’t set properties outside transactions, you can’t move it across threads, it gets automatically updated when the Realm is refreshed, etc.).
Yes - your example is almost correct, the difference being that thaw returns a live version of the object rather than thawing it in place. So in your second snippet, you’ll need something like:
let frozenDog = self.myDog.freeze()
// ---> pass frozenDog to other viewController
let thawedDog = frozenDog.thaw()
try! realm.write {
thawedDog.age = 102
}
// If the write succeeded, self.myDog in the first viewController
// should see its age updated to 102.