Skip to content

Commit 136e731

Browse files
authored
(DOCSP-25168): Add note about MainActor in async/await realm examples (#2165)
* Add note about MainActor in async/await realm examples * Remove new tip where it's not needed
1 parent eb5035e commit 136e731

File tree

8 files changed

+33
-0
lines changed

8 files changed

+33
-0
lines changed

source/includes/swift-async-await-support.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@ meet these requirements:
1818
* - 10.15.0 or 10.16.0
1919
- Swift 5.5
2020
- iOS 15.x
21+
22+
If your app accesses Realm in an ``async/await`` context, mark the code
23+
with ``@MainActor`` to avoid threading-related crashes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.. tip::
2+
3+
If your app accesses Realm in an ``async/await`` context, mark the code
4+
with ``@MainActor`` to avoid threading-related crashes.

source/sdk/swift/crud/threading.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,8 @@ and results, on a struct or class property, or on a function parameter.
283283
parameter. This is useful for functions that may run asynchronously
284284
or on another thread.
285285

286+
.. include:: /includes/swift-concurrency-mainactor.rst
287+
286288
.. literalinclude:: /examples/generated/code/start/Threading.snippet.threadsafe-wrapper-function-parameter.swift
287289
:language: swift
288290

source/sdk/swift/quick-start.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ what data the realm can read and write.
159159
Once you have a realm with a subscription, this example passes the realm
160160
and the user to another function where you can use the realm.
161161

162+
.. include:: /includes/swift-concurrency-mainactor.rst
163+
162164
.. literalinclude:: /examples/generated/code/start/QuickStartFlexSync.snippet.open-synced-realm.swift
163165
:language: swift
164166

source/sdk/swift/realm-files/bundle-a-realm.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ Create a Realm File for Bundling
8383
#. Use the :swift-sdk:`writeCopy(configuration:) <Structs/Realm.html#/s:10RealmSwift0A0V9writeCopy13configurationyAC13ConfigurationV_tKF>`
8484
method to copy the realm to a new file:
8585

86+
.. include:: /includes/swift-concurrency-mainactor.rst
87+
8688
.. literalinclude:: /examples/generated/code/start/BundleRealms.snippet.copy-synced-realm-for-bundling.swift
8789
:language: swift
8890

@@ -139,6 +141,8 @@ application, you need to add code to use it. Use the :swift-sdk:`seedFilePath
139141
method when configuring your realm to open the realm
140142
from the bundled file:
141143

144+
.. include:: /includes/swift-concurrency-mainactor.rst
145+
142146
.. literalinclude:: /examples/generated/code/start/BundleRealms.snippet.open-bundled-synced-realm.swift
143147
:language: swift
144148

source/sdk/swift/sync/add-sync-to-app.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ Add Device Sync to a Client Application
7171
types <ios-provide-a-subset-of-classes-to-a-realm>` that we want to
7272
include in this realm.
7373

74+
.. include:: /includes/swift-concurrency-mainactor.rst
75+
7476
.. literalinclude:: /examples/generated/code/start/AddSyncToApp.snippet.open-synced-realm.swift
7577
:language: swift
7678

source/sdk/swift/sync/configure-and-open-a-synced-realm.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ Open a Synced Realm for Flexible Sync
167167
When you use Flexible Sync, use the ``flexibleSyncConfiguration()``
168168
to open a synced realm.
169169

170+
.. include:: /includes/swift-concurrency-mainactor.rst
171+
170172
.. literalinclude:: /examples/generated/code/start/FlexibleSync.snippet.flex-sync-open-realm.swift
171173
:language: swift
172174

@@ -195,6 +197,8 @@ open the copy as a synced realm for that user.
195197

196198
.. include:: /includes/note-writecopy-pbs-only.rst
197199

200+
.. include:: /includes/swift-concurrency-mainactor.rst
201+
198202
.. literalinclude:: /examples/generated/code/start/ConvertSyncLocalRealms.snippet.convert-sync-to-sync.swift
199203
:language: swift
200204

@@ -219,6 +223,8 @@ the App Services backend.
219223

220224
.. include:: /includes/note-writecopy-pbs-only.rst
221225

226+
.. include:: /includes/swift-concurrency-mainactor.rst
227+
222228
.. literalinclude:: /examples/generated/code/start/ConvertSyncLocalRealms.snippet.convert-local-to-sync.swift
223229
:language: swift
224230

@@ -250,6 +256,8 @@ non-synced realm. Any changes you make to the non-synced realm reflect
250256
only in the local realm file. No changes propogate to other devices or
251257
the App Services backend.
252258

259+
.. include:: /includes/swift-concurrency-mainactor.rst
260+
253261
.. literalinclude:: /examples/generated/code/start/ConvertSyncLocalRealms.snippet.convert-sync-to-local.swift
254262
:language: swift
255263

@@ -275,6 +283,8 @@ This parameter accepts a case from the ``OpenBehavior`` enum:
275283
populate a realm with initial data, but enables offline-first
276284
functionality on subsequent opens.
277285

286+
.. include:: /includes/swift-concurrency-mainactor.rst
287+
278288
.. include:: /examples/generated/code/start/Sync.snippet.specify-download-behavior.swift.rst
279289

280290
.. _ios-open-a-synced-realm-offline:
@@ -288,5 +298,7 @@ You can only open a synced realm offline if you do not require your client
288298
app to ``always`` :ref:`download changes before opening the realm
289299
<ios-specify-download-behavior>`.
290300

301+
.. include:: /includes/swift-concurrency-mainactor.rst
302+
291303
.. literalinclude:: /examples/generated/code/start/Authenticate.snippet.offline-login.swift
292304
:language: swift

source/sdk/swift/sync/flexible-sync.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ Add a Subscription
138138
Add a subscription in a subscriptions update block. You append each
139139
new subscription to the client's Realm subscriptions.
140140

141+
.. include:: /includes/swift-concurrency-mainactor.rst
142+
141143
.. literalinclude:: /examples/generated/code/start/FlexibleSync.snippet.add-single-subscription.swift
142144
:language: swift
143145

@@ -252,6 +254,8 @@ error if the update cannot complete successfully.
252254
}
253255
}
254256

257+
.. include:: /includes/swift-concurrency-mainactor.rst
258+
255259
SyncSubscriptionState Enum
256260
``````````````````````````
257261

0 commit comments

Comments
 (0)