-
-
Notifications
You must be signed in to change notification settings - Fork 121
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
Summarize internal UserDefaults
observation class
#181
Conversation
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. |
c9971b8
to
eecf057
Compare
Got your point! |
eecf057
to
dfdddca
Compare
Sources/Defaults/Observation.swift
Outdated
|
||
init(object: UserDefaults, key: String, callback: @escaping Callback) { | ||
self.object = object | ||
init(suite: NSObject, key: String) { |
There was a problem hiding this comment.
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?
init(suite: NSObject, key: String) { | |
init(suite: UserDefaults, key: String) { |
Sources/Defaults/Observation.swift
Outdated
private let callback: Callback | ||
private var isObserving = false | ||
final class SuiteKeyPair: Hashable { | ||
weak var suite: NSObject? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
weak var suite: NSObject? | |
weak var suite: UserDefaults? |
Sources/Defaults/Observation.swift
Outdated
private weak var object: UserDefaults? | ||
private let key: String | ||
static var observationContext = 0 | ||
private weak var suite: NSObject? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
private weak var suite: NSObject? | |
private weak var suite: UserDefaults? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed! Thanks!
Sources/Defaults/Observation.swift
Outdated
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 | ||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
👍
2031332
to
fbf740b
Compare
Looks great now 👍 |
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 withDefaults.iCloud
).