Skip to content

Commit 3c0d9a4

Browse files
committed
A few more fixes
1 parent ce17215 commit 3c0d9a4

File tree

1 file changed

+4
-63
lines changed

1 file changed

+4
-63
lines changed

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

Lines changed: 4 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ struct Feature {
517517
}
518518
```
519519

520-
This feature can be tested in exactly the same way as when you are using non-shared state:
520+
This feature can be tested in a similar same way as when you are using non-shared state:
521521

522522
```swift
523523
@Test
@@ -527,7 +527,7 @@ func increment() async {
527527
}
528528

529529
await store.send(.incrementButtonTapped) {
530-
$0.count = 1
530+
$0.$count.withLock { $0 = 1 }
531531
}
532532
}
533533
```
@@ -544,7 +544,7 @@ func increment() async {
544544
}
545545

546546
await store.send(.incrementButtonTapped) {
547-
$0.count = 2
547+
$0.$count.withLock { $0 = 2 }
548548
}
549549
}
550550
```
@@ -623,7 +623,7 @@ func increment() async {
623623
}
624624
await store.send(.incrementButtonTapped)
625625
store.assert {
626-
$0.count = 1
626+
$0.$count.withLock { $0 = 1 }
627627
}
628628
}
629629
```
@@ -1050,62 +1050,3 @@ extension AppState: Codable {
10501050
}
10511051
}
10521052
```
1053-
1054-
#### Previews
1055-
1056-
When a preview is run in an app target, the entry point is also created. This means if your entry
1057-
point looks something like this:
1058-
1059-
```swift
1060-
@main
1061-
struct MainApp: App {
1062-
let store = Store()
1063-
1064-
var body: some Scene {
1065-
1066-
}
1067-
}
1068-
```
1069-
1070-
then a store will be created each time you run your preview. This can be problematic with `@Shared`
1071-
and persistence strategies because the first access of a `@Shared` property will use the default
1072-
value provided, and that will cause `@Shared`'s created later to ignore the default. That will mean
1073-
you cannot override shared state in previews.
1074-
1075-
The fix is to delay creation of the store until the entry point's `body` is executed. Further, it
1076-
can be a good idea to also not run the `body` when in tests because that can also interfere with
1077-
tests (as documented in <doc:Testing#Testing-gotchas>). Here is one way this can be accomplished:
1078-
1079-
```swift
1080-
import ComposableArchitecture
1081-
import SwiftUI
1082-
1083-
@main
1084-
struct MainApp: App {
1085-
@MainActor
1086-
static let store = Store()
1087-
1088-
var body: some Scene {
1089-
WindowGroup {
1090-
if isTesting {
1091-
// NB: Don't run application in tests to avoid interference
1092-
// between the app and the test.
1093-
EmptyView()
1094-
} else {
1095-
AppView(store: Self.store)
1096-
}
1097-
}
1098-
}
1099-
}
1100-
```
1101-
1102-
Alternatively you can take an extra step to override shared state in your previews:
1103-
1104-
```swift
1105-
#Preview {
1106-
@Shared(.appStorage("isOn")) var isOn = true
1107-
isOn = true
1108-
}
1109-
```
1110-
1111-
The second assignment of `isOn` will guarantee that it holds a value of `true`.

0 commit comments

Comments
 (0)