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

Summarize internal UserDefaults observation class #181

Conversation

hank121314
Copy link
Collaborator

Description

This PR fixes: #170.
Summarize all internal UserDefaults related observation classes into one.
Introduce a new class, Defaults.UserDefaultsAnyKeysObservation, which can observe multiple keys and can add or remove the keys being observed(useful with Defaults.iCloud).

@hank121314 hank121314 requested a review from sindresorhus July 20, 2024 07:21
@sindresorhus
Copy link
Owner

As outlined in #136 (comment), I was looking to have class that handles the minimum amount of logic for the "low-level" observation handling for a single key, and then rather have another class that handles the high-level logic with multiple keys. It's generally beneficial to wrap low-level/legacy code in a wrapper to make it easier to reason about.

@hank121314 hank121314 force-pushed the refactor/summarize-userdefaults-observation branch 2 times, most recently from c9971b8 to eecf057 Compare July 27, 2024 12:19
@hank121314
Copy link
Collaborator Author

As outlined in #136 (comment), I was looking to have class that handles the minimum amount of logic for the "low-level" observation handling for a single key, and then rather have another class that handles the high-level logic with multiple keys. It's generally beneficial to wrap low-level/legacy code in a wrapper to make it easier to reason about.

Got your point!
Yeah, it is really good to wrap low-level/legacy code in a wrapper. It also makes it possible for me to focus on the high-level logic 👍.
Many thanks for the suggestion!

@hank121314 hank121314 force-pushed the refactor/summarize-userdefaults-observation branch from eecf057 to dfdddca Compare July 27, 2024 12:24

init(object: UserDefaults, key: String, callback: @escaping Callback) {
self.object = object
init(suite: NSObject, key: String) {
Copy link
Owner

Choose a reason for hiding this comment

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

Why not use the specific type?

Suggested change
init(suite: NSObject, key: String) {
init(suite: UserDefaults, key: String) {

private let callback: Callback
private var isObserving = false
final class SuiteKeyPair: Hashable {
weak var suite: NSObject?
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
weak var suite: NSObject?
weak var suite: UserDefaults?

private weak var object: UserDefaults?
private let key: String
static var observationContext = 0
private weak var suite: NSObject?
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
private weak var suite: NSObject?
private weak var suite: UserDefaults?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed! Thanks!

Comment on lines 179 to 186
func invalidate() {
if isObserving {
object?.removeObserver(self, forKeyPath: key, context: nil)
isObserving = false
guard isObserving else {
return
}

object = nil
suite?.removeObserver(self, forKeyPath: name)
isObserving = false
suite = nil
}
Copy link
Owner

Choose a reason for hiding this comment

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

Unrelated to this specific pull request, but I wonder if this is thread-safe. It may be smart to put a lock on it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Put a lock on it. Maybe we should consider to use AtomicBoolean after swift 6?

Copy link
Owner

Choose a reason for hiding this comment

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

Maybe we should consider to use AtomicBoolean after swift 6?

👍

@hank121314 hank121314 force-pushed the refactor/summarize-userdefaults-observation branch from 2031332 to fbf740b Compare August 2, 2024 10:30
@sindresorhus sindresorhus merged commit bf71746 into sindresorhus:main Aug 2, 2024
2 checks passed
@sindresorhus
Copy link
Owner

Looks great now 👍

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.

Improve UserDefaultsKeyObservation
2 participants