You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It looks that AppStorage should migrate from NSUserDefaultsDidChangeNotification to key-value observing for specific keys. According to Apple's documentation.
You can use key-value observing to register observers for specific keys of interest in order to be notified of all updates, regardless of whether changes are made within or outside the current process.
Checklist
I have determined whether this bug is also reproducible in a vanilla SwiftUI project.
If possible, I've reproduced the issue using the main branch of this package.
larryonoff
changed the title
@Shared(.appStorage) updates are triggered whenever any UserDefaults key changes, not just a specific key.
@Shared(.appStorage) updates are triggered whenever any UserDefaults key changes, not just a specific key
Jun 18, 2024
We can't subscribe to the changes for just a single key in UserDefaults because KVO treats "compound" keys (i.e. "foo.bar") in a special way, and it that breaks observation if you were ever to do .appStorage("feature.count"). We have no choice but to subscribe to the firehose of all user default changes.
However, because user defaults can only store very basic data, we can implement some simple remove duplicates logic.
We can't subscribe to the changes for just a single key in UserDefaults because KVO treats "compound" keys (i.e. "foo.bar") in a special way, and it that breaks observation if you were ever to do .appStorage("feature.count"). We have no choice but to subscribe to the firehose of all user default changes.
However, because user defaults can only store very basic data, we can implement some simple remove duplicates logic.
Thanks for the fix. Looking forward to the new release with the latest fixes!
PS. Sad that it's not possible to fix the issue in an optimal way.
Description
It looks that AppStorage should migrate from NSUserDefaultsDidChangeNotification to key-value observing for specific keys. According to Apple's documentation.
Checklist
main
branch of this package.Expected behavior
AppStorage triggers updates only when specific key changes.
Actual behavior
AppStorage triggers updates whenever any change occurs in NSUserDefaults.
Steps to reproduce
Check the sample project
The Composable Architecture version information
1.11.1
Destination operating system
iOS 17.5.1
Xcode version information
Version 15.4 (15F31d)
Swift Compiler version information
The text was updated successfully, but these errors were encountered: