Skip to content

How does _AppStorage interoperate with SwiftUI AppStorage? #32

Answered by tgrapperon
Zeta611 asked this question in Q&A
Discussion options

You must be logged in to vote

Hey @Zeta611! Sorry if this wasn't clear: they interoperate only with the default live implementation. In your case, you're using the ephemeral storage, but the SwiftUI variant doesn't know anything about that. They interoperate when you don't override userDefault storage (or use the same UserDefaults instance), because they're writing to the same place, in the same fashion, and they both observe changes to this storage. It means that you can create a Settings model that writes to @Dependency.AppStorage, and pop this preference in any View elsewhere in the hierarchy using SwiftUI's @AppStorage for example.

It is partly working here because you defined your View in a withDependency block, …

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Answer selected by Zeta611
Comment options

You must be logged in to vote
2 replies
@tgrapperon
Comment options

@Zeta611
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants