Freeze (Frozen Objects) and Thawing

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)

If not, what’s going on under the hood.

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.

1 Like

Ok so how does one interact with the thawed object?

Suppose - per the doc - that an object is frozen, passed to a different view and then thawed. To which then one of the properties is updated.

Is this now an unmanaged object that does not exist in Realm but has all of the same property values (including the primary key).

Then to write it out

try! realm.write {
   realm.add(thawedObject, update: .modified)
}

So then both in memory objects (now managed) point to the same (on disk) object?

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.).

1 Like

@nirinchev

Thank you for the additional info. I want to make sure we understand the premise before implementation:

So it’s the same object and NOT a copy?

Let me use a real world example for clarity.

Suppose I have a viewController with a class var of DogClass.

self.myDog = realm.object(ofType: DogClass. self, forPrimaryKey: "1234")

another viewController is instantiated and passed a frozen dog

self.myDog.freeze
---> pass to other viewController

then I need to modify the dogs age so I thaw it in the other viewController

self.myDog.thaw
try! realm.write {
    self.myDog.age = 102
}

Will that write will reflect back to the self.myDog in the first viewController?

If not, more clarification will be needed so forgive my continual questions.

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.
1 Like