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

@Shared(.appStorage) updates are triggered whenever any UserDefaults key changes, not just a specific key #3179

Closed
3 tasks done
larryonoff opened this issue Jun 18, 2024 · 2 comments · Fixed by #3186
Closed
3 tasks done
Labels
bug Something isn't working due to a bug in the library.

Comments

@larryonoff
Copy link
Contributor

Description

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.
  • This issue hasn't been addressed in an existing GitHub issue or discussion.

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

swift-driver version: 1.90.11.1 Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)
Target: arm64-apple-macosx14.0
@larryonoff larryonoff added the bug Something isn't working due to a bug in the library. label Jun 18, 2024
@larryonoff 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
@mbrandonw
Copy link
Member

Hi @larryonoff, this will be fixed in #3186.

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.

@larryonoff
Copy link
Contributor Author

larryonoff commented Jun 18, 2024

Hi @larryonoff, this will be fixed in #3186.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working due to a bug in the library.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants