Replies: 2 comments
-
Hi @hiltonc, this is happening for a combination of reasons in your code. It's due to setting the init(loadState: LoadState<String?, Error> = .loading) {
defer {
self.loadState = loadState
}
} …and due to the var loadState: LoadState<String?, Error> = .loading {
didSet {
if case let .loaded(loadedModel) = loadState {
data = loadedModel
} else if case .failure = loadState {
data = ""
}
}
} Setting And so then later in the view when It's not a perfect system, but it's the best we've come up with. Detecting field access in a SwiftUI body without However, in general it is quite precarious to access observable fields directly in an initailizer or So, to fix you should access the underscored field in var loadState: LoadState<String?, Error> = .loading {
didSet {
if case let .loaded(loadedModel) = _loadState {
data = loadedModel
} else if case .failure = _loadState {
data = ""
}
}
} And same with the initializer: init(loadState: LoadState<String?, Error> = .loading) {
defer {
_loadState = loadState
}
} If you have ideas to improve the perception checking to be more precise and/or efficient we'd happily take a PR! Since this isn't an issue with the library I am going to convert this to a discussion. |
Beta Was this translation helpful? Give feedback.
-
Thank you for your quick response on this. It's a bummer that we can't rely on the perception check warnings completely, but I understand the tradeoff. This also does seem to be a strange outlier, as I continue to migrate our code I haven't run into another case yet. |
Beta Was this translation helpful? Give feedback.
-
Description
While migrating some app code to use Perception I ran into a case where I did not get a perception check warning but should have. This is concerning because we're depending on these warnings to ensure that the feature will work on older iOS versions while doing development on iOS 17.
Checklist
@Observable
macro or another tool from theObservation
framework, please file it directly with Apple.main
branch of this package.Expected behavior
When running on iOS 17 I expect to get a perception check warning for not using
WithPerceptionTracking
.Actual behavior
Observation works on iOS 17 (text goes from "loading" to "loaded"), but there is no perception check warning. As expected, on iOS 16 the text stays at "loading" because
WithPerceptionTracking
was not used.Steps to reproduce
Perception version information
1.1.2
Destination operating system
iOS 17
Xcode version information
Xcode 15.2
Swift Compiler version information
Beta Was this translation helpful? Give feedback.
All reactions