Skip to content

Commit 3405b9c

Browse files
authored
(DOCSP-15555): Add "thaw" to frozen object documentation (#1008)
1 parent 9873fa5 commit 3405b9c

File tree

5 files changed

+170
-110
lines changed

5 files changed

+170
-110
lines changed

examples/ios/Examples/Threading.swift

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,47 @@ class Threading: XCTestCase {
9191
// :code-block-end:
9292
}
9393

94+
func testThaw() {
95+
let realm = try! Realm()
96+
97+
try! realm.write {
98+
realm.add(Task())
99+
}
100+
101+
let frozenRealm = realm.freeze()
102+
103+
// :code-block-start: thaw
104+
// Read from a frozen realm
105+
let frozenTasks = frozenRealm.objects(Task.self)
106+
107+
// The collection that we pull from the frozen realm is also frozen
108+
assert(frozenTasks.isFrozen)
109+
110+
// Get an individual task from the collection
111+
let frozenTask = frozenTasks.first!
112+
113+
// To modify the task, you must first thaw it
114+
// You can also thaw collections and realms
115+
let thawedTask = frozenTask.thaw()
116+
117+
// Check to make sure this task is valid. An object is
118+
// invalidated when it is deleted from its managing realm,
119+
// or when its managing realm has invalidate() called on it.
120+
assert(thawedTask?.isInvalidated == false)
121+
122+
// Thawing the task also thaws the frozen realm it references
123+
assert(thawedTask!.realm!.isFrozen == false)
124+
125+
// Let's make the code easier to follow by naming the thawed realm
126+
let thawedRealm = thawedTask!.realm!
127+
128+
// Now, you can modify the task
129+
try! thawedRealm.write {
130+
thawedTask!.status = "Done"
131+
}
132+
// :code-block-end:
133+
}
134+
94135
func testPassAcrossThreads() {
95136
let expectation = XCTestExpectation(description: "it completes")
96137
// :code-block-start: pass-across-threads

0 commit comments

Comments
 (0)