Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Record state access when constructing _StoreCollection. #3521

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mbrandonw
Copy link
Member

This fixes an issue brought up in #3518. When constructing a _StoreCollection, like for a ForEach, we should record a state access with the registrar. Currently we use withState, which silently accesses state, and so one will not get perception warnings if they forget to use WithPerceptionTracking.

Comment on lines +103 to 104
store._$observationRegistrar.access(store, keyPath: \.currentState)
self.data = store.withState { $0 }
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This definitely fixes the problem, but I am also wondering if we should be dropping the _withoutPerceptionChecking from withState as an alternative fix. We introduced withState back in the view store days to give people easy access to state when observation wasn't needed. But now with implicit observation everywhere, should we be tracking those accesses with the registrar?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only reason not to do this that I can think of is that it's a big behavior change for folks not using @ObservableState, and they could get really inefficient SwiftUI observation wherever they decide to use withState. I think 2.0 will give us a nice opportunity here to opt into @ObservableState by default and avoid these questions entirely.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants