Skip to content

Commit 95d3b3e

Browse files
committed
Clean up from #61
- Refine docs and assert messages - Apply swiftlint fixes - Avoid possible retain cycle
1 parent 8c3843d commit 95d3b3e

File tree

3 files changed

+22
-15
lines changed

3 files changed

+22
-15
lines changed

Sources/ObserverTrampoline.swift

+20-13
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,39 @@
1313

1414
import Foundation
1515

16-
/// Watches for changes to UserDefaults using old-school Key-Value Observing.
16+
/// Watches for changes to `UserDefaults` using old-school Key-Value Observing.
1717
///
18-
/// KVO allows us to be notified of changes from anywhere (including other processes), not just via the property wrapper's setter.
18+
/// KVO allows us to be notified of changes from anywhere (including other processes),
19+
/// not just via the property wrapper's setter.
1920
///
20-
/// We can't use Swift's block-based KVO because that requires a KeyPath, which we cannot create from a String.
21+
/// We can't use Swift's block-based KVO because that requires a `KeyPath`,
22+
/// which we cannot create from a `String`.
2123
final class ObserverTrampoline: NSObject {
2224
private let userDefaults: UserDefaults
2325
private let key: String
24-
private let block: () -> Void
26+
private let action: () -> Void
2527

26-
init(userDefaults: UserDefaults, key: String, block: @escaping () -> Void) {
27-
assert(!key.hasPrefix("@"), "Cannot observe a user default key starting with '@'")
28-
assert(!key.contains("."), "Cannot observe a user default key containing '.'")
28+
init(userDefaults: UserDefaults, key: String, action: @escaping () -> Void) {
29+
assert(!key.hasPrefix("@"), "Error: key name cannot begin with a '@' character and be observed via KVO.")
30+
assert(!key.contains("."), "Error: key name cannot contain a '.' character anywhere and be observed via KVO.")
2931
self.userDefaults = userDefaults
3032
self.key = key
31-
self.block = block
33+
self.action = action
3234
super.init()
33-
3435
userDefaults.addObserver(self, forKeyPath: key, context: nil)
3536
}
3637

37-
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) {
38-
block()
38+
deinit {
39+
self.userDefaults.removeObserver(self, forKeyPath: self.key, context: nil)
3940
}
4041

41-
deinit {
42-
userDefaults.removeObserver(self, forKeyPath: key, context: nil)
42+
// swiftlint:disable:next block_based_kvo
43+
override func observeValue(
44+
forKeyPath keyPath: String?,
45+
of object: Any?,
46+
// swiftlint:disable:next discouraged_optional_collection
47+
change: [NSKeyValueChangeKey: Any]?,
48+
context: UnsafeMutableRawPointer?) {
49+
self.action()
4350
}
4451
}

Sources/WrappedDefault.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public struct WrappedDefault<T: UserDefaultsSerializable> {
5555
// and uses force unwrap
5656
self._publisher = CurrentValueSubject<T, Never>(userDefaults.fetch(keyName))
5757

58-
self._observer = ObserverTrampoline(userDefaults: userDefaults, key: keyName) { [_publisher] in
58+
self._observer = ObserverTrampoline(userDefaults: userDefaults, key: keyName) { [unowned _publisher] in
5959
_publisher.send(userDefaults.fetch(keyName))
6060
}
6161
}

Sources/WrappedDefaultOptional.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public struct WrappedDefaultOptional<T: UserDefaultsSerializable> {
5252
self.key = keyName
5353
self._userDefaults = userDefaults
5454
self._publisher = CurrentValueSubject<T?, Never>(userDefaults.fetchOptional(keyName))
55-
self._observer = ObserverTrampoline(userDefaults: userDefaults, key: keyName) { [_publisher] in
55+
self._observer = ObserverTrampoline(userDefaults: userDefaults, key: keyName) { [unowned _publisher] in
5656
_publisher.send(userDefaults.fetchOptional(keyName))
5757
}
5858
}

0 commit comments

Comments
 (0)