Skip to content

Commit bc67aa8

Browse files
authored
Update messaging for perception warning. (#96)
1 parent 1552c8f commit bc67aa8

File tree

6 files changed

+35
-9
lines changed

6 files changed

+35
-9
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,10 @@ It's unfortunate to have to wrap your view's content in `WithPerceptionTracking`
6666
forget then you will helpfully get a runtime warning letting you know that observation is not
6767
set up correctly:
6868

69-
> 🟣 Runtime Warning: Perceptible state was accessed but is not being tracked. Track changes to
70-
> state by wrapping your view in a 'WithPerceptionTracking' view.
69+
> 🟣 Runtime Warning: Perceptible state was accessed but is not being tracked. Track changes
70+
> to state by wrapping your view in a 'WithPerceptionTracking' view. This must also be done
71+
> for any escaping, trailing closures, such as 'GeometryReader', `LazyVStack` (and all lazy
72+
> views), navigation APIs ('sheet', 'popover', 'fullScreenCover', etc.), and others.
7173
7274
### Bindable
7375

Sources/Perception/Internal/Locals.swift

Lines changed: 0 additions & 4 deletions
This file was deleted.

Sources/Perception/PerceptionChecking.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
11
import Foundation
22

3+
/// Globally enable or disable perception checks.
4+
///
5+
/// The library performs certain runtime checks to make sure that the tools are being used
6+
/// correctly. In particular, view bodies must be wrapped in the ``WithPerceptionTracking`` view
7+
/// in order for observation to be properly tracked. If the library detects state is accessed
8+
/// without being inside ``WithPerceptionTracking``, a runtime warning is triggered to let you
9+
/// know there is something to fix.
10+
///
11+
/// This check only happens in `DEBUG` builds, and so does not affect App Store releases of your
12+
/// app. However, the checks can sometimes be costly and slow down your app in development. If
13+
/// you wish to fully disable the checks, you can set this boolean to `false.`
314
public var isPerceptionCheckingEnabled: Bool {
415
get { perceptionChecking.isPerceptionCheckingEnabled }
516
set { perceptionChecking.isPerceptionCheckingEnabled = newValue }
617
}
718

19+
public enum _PerceptionLocals {
20+
@TaskLocal public static var isInPerceptionTracking = false
21+
@TaskLocal public static var skipPerceptionChecking = false
22+
}
23+
824
private let perceptionChecking = PerceptionChecking()
925

1026
private class PerceptionChecking: @unchecked Sendable {

Sources/Perception/PerceptionRegistrar.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,9 @@ extension PerceptionRegistrar: Hashable {
214214
reportIssue(
215215
"""
216216
Perceptible state was accessed but is not being tracked. Track changes to state by \
217-
wrapping your view in a 'WithPerceptionTracking' view.
217+
wrapping your view in a 'WithPerceptionTracking' view. This must also be done for any \
218+
escaping, trailing closures, such as 'GeometryReader', `LazyVStack` (and all lazy \
219+
views), navigation APIs ('sheet', 'popover', 'fullScreenCover', etc.), and others.
218220
"""
219221
)
220222
}

Sources/Perception/WithPerceptionTracking.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,19 @@
3030
/// }
3131
/// ```
3232
///
33+
/// > Note: Other common escaping closures to be aware of:
34+
/// > * Reader views, such as `GeometryReader`, ScrollViewReader`, etc.
35+
/// > * Lazy views such as `LazyVStack`, `LazyVGrid`, etc.
36+
/// > * Navigation APIs, such as `sheet`, `popover`, `fullScreenCover`, `navigationDestination`,
37+
/// etc.
38+
///
3339
/// If a field of a `@Perceptible` model is accessed in a view while _not_ inside
3440
/// ``WithPerceptionTracking``, then a runtime warning will helpfully be triggered:
3541
///
3642
/// > 🟣 Runtime Warning: Perceptible state was accessed but is not being tracked. Track changes
37-
/// > to state by wrapping your view in a 'WithPerceptionTracking' view.
43+
/// > to state by wrapping your view in a 'WithPerceptionTracking' view. This must also be done
44+
/// > for any escaping, trailing closures, such as 'GeometryReader', `LazyVStack` (and all lazy
45+
/// > views), navigation APIs ('sheet', 'popover', 'fullScreenCover', etc.), and others.
3846
///
3947
/// To debug this, expand the warning in the Issue Navigator of Xcode (cmd+5), and click through
4048
/// the stack frames displayed to find the line in your view where you are accessing state without

Tests/PerceptionTests/RuntimeWarningTests.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,9 @@
613613
XCTExpectFailure(failingBlock: failingBlock) {
614614
$0.compactDescription == """
615615
failed - Perceptible state was accessed but is not being tracked. Track changes to state \
616-
by wrapping your view in a 'WithPerceptionTracking' view.
616+
by wrapping your view in a 'WithPerceptionTracking' view. This must also be done for any \
617+
escaping, trailing closures, such as 'GeometryReader', `LazyVStack` (and all lazy \
618+
views), navigation APIs ('sheet', 'popover', 'fullScreenCover', etc.), and others.
617619
"""
618620
}
619621
}

0 commit comments

Comments
 (0)