Skip to content

Commit 889c27b

Browse files
authored
Note Swift bug in documentation (#3157)
It'd be good to have a place to point folks to when encountering the issue.
1 parent aa56697 commit 889c27b

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Sources/ComposableArchitecture/Documentation.docc/Articles/SharingState.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,28 @@ problems due to re-entrancy. To avoid problems like the above we recommend wrapp
10201020
mutations of the shared state as possible in a single ``Shared/withLock(_:)``. That will make
10211021
sure that the full unit of work is guarded by a lock.
10221022

1023+
> Note: You may encounter a deprecation warning when simply _accessing_ shared state from an
1024+
> asynchronous context when you chain into a subscript:
1025+
>
1026+
> ```swift
1027+
> return .run { _ in
1028+
> @Shared(.posts) var posts
1029+
> let post = posts[id: id] // ⚠️ Setter is unavailable from asynchronous contexts
1030+
> // ...
1031+
> }
1032+
> ```
1033+
>
1034+
> This is a [known issue](https://github.com/apple/swift/issues/74203) in the Swift compiler, but
1035+
> can be worked around using ``Shared/withLock(_:)`` to access the underlying value instead:
1036+
>
1037+
> ```swift
1038+
> return .run { _ in
1039+
> @Shared(.posts) var posts
1040+
> let post = $posts.withLock { $0[id: id] }
1041+
> // ...
1042+
> }
1043+
> ```
1044+
10231045
## Gotchas of @Shared
10241046

10251047
There are a few gotchas to be aware of when using shared state in the Composable Architecture.

0 commit comments

Comments
 (0)