Skip to content

Commit ff574be

Browse files
committed
Manage observer notification tokens explicitly
1 parent a59cac5 commit ff574be

9 files changed

Lines changed: 80 additions & 40 deletions

Sources/ObservableDefaultsMacros/Helper/ObserverMembers.swift

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ func makeDefaultsObserverMembers(
117117
let userDefaults: Foundation.UserDefaults
118118
let prefix: String
119119
let observableKeysBlacklist: [String]
120+
private var notificationObserver: NSObjectProtocol?
120121
121122
/// Initializes the observation with the specified parameters.
122123
/// - Parameters:
@@ -130,13 +131,14 @@ func makeDefaultsObserverMembers(
130131
self.prefix = prefix
131132
self.observableKeysBlacklist = observableKeysBlacklist
132133
133-
NotificationCenter.default
134+
notificationObserver = NotificationCenter.default
134135
.addObserver(
135136
forName: UserDefaults.didChangeNotification,
136137
object: \(raw: limitToInstance ? "userDefaults" : "nil"),
137138
queue: nil,
138-
using: userDefaultsDidChange
139-
)
139+
using: { [weak self] notification in
140+
self?.userDefaultsDidChange(notification)
141+
})
140142
}
141143
142144
/// Handles UserDefaults changes from external sources.
@@ -163,7 +165,9 @@ func makeDefaultsObserverMembers(
163165
}
164166
165167
deinit {
166-
NotificationCenter.default.removeObserver(self)
168+
if let observer = notificationObserver {
169+
NotificationCenter.default.removeObserver(observer)
170+
}
167171
}
168172
}
169173
"""
@@ -261,6 +265,7 @@ func makeCloudObserverSyntax(
261265
private final class CloudObservation: @unchecked Sendable {
262266
weak var host: \(className)?
263267
let prefix: String
268+
private var notificationObserver: NSObjectProtocol?
264269
265270
/// Initializes the observation with the specified parameters.
266271
/// - Parameters:
@@ -269,13 +274,14 @@ func makeCloudObserverSyntax(
269274
init(host: \(className), prefix: String) {
270275
self.host = host
271276
self.prefix = prefix
272-
NotificationCenter.default
277+
notificationObserver = NotificationCenter.default
273278
.addObserver(
274279
forName: NSUbiquitousKeyValueStore.didChangeExternallyNotification,
275280
object: nil,
276281
queue: nil,
277-
using: cloudStoreDidChange
278-
)
282+
using: { [weak self] notification in
283+
self?.cloudStoreDidChange(notification)
284+
})
279285
}
280286
281287
/// Handles cloud store changes from external sources.
@@ -300,7 +306,9 @@ func makeCloudObserverSyntax(
300306
}
301307
302308
deinit {
303-
NotificationCenter.default.removeObserver(self)
309+
if let observer = notificationObserver {
310+
NotificationCenter.default.removeObserver(observer)
311+
}
304312
}
305313
}
306314
"""

Tests/ObservableDefaultsMacroTests/__Snapshots__/ObservableCloudObserveFirst.expanded.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ final class CloudObserveFirstFixture {
146146
private final class CloudObservation: @unchecked Sendable {
147147
weak var host: CloudObserveFirstFixture?
148148
let prefix: String
149+
private var notificationObserver: NSObjectProtocol?
149150

150151
/// Initializes the observation with the specified parameters.
151152
/// - Parameters:
@@ -154,13 +155,14 @@ final class CloudObserveFirstFixture {
154155
init(host: CloudObserveFirstFixture, prefix: String) {
155156
self.host = host
156157
self.prefix = prefix
157-
NotificationCenter.default
158+
notificationObserver = NotificationCenter.default
158159
.addObserver(
159160
forName: NSUbiquitousKeyValueStore.didChangeExternallyNotification,
160161
object: nil,
161162
queue: nil,
162-
using: cloudStoreDidChange
163-
)
163+
using: { [weak self] notification in
164+
self?.cloudStoreDidChange(notification)
165+
})
164166
}
165167

166168
/// Handles cloud store changes from external sources.
@@ -185,7 +187,9 @@ final class CloudObserveFirstFixture {
185187
}
186188

187189
deinit {
188-
NotificationCenter.default.removeObserver(self)
190+
if let observer = notificationObserver {
191+
NotificationCenter.default.removeObserver(observer)
192+
}
189193
}
190194
}
191195

Tests/ObservableDefaultsMacroTests/__Snapshots__/ObservableCloudObserveFirst.formatted.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ final class CloudObserveFirstFixture {
146146
private final class CloudObservation: @unchecked Sendable {
147147
weak var host: CloudObserveFirstFixture?
148148
let prefix: String
149+
private var notificationObserver: NSObjectProtocol?
149150

150151
/// Initializes the observation with the specified parameters.
151152
/// - Parameters:
@@ -154,13 +155,14 @@ final class CloudObserveFirstFixture {
154155
init(host: CloudObserveFirstFixture, prefix: String) {
155156
self.host = host
156157
self.prefix = prefix
157-
NotificationCenter.default
158+
notificationObserver = NotificationCenter.default
158159
.addObserver(
159160
forName: NSUbiquitousKeyValueStore.didChangeExternallyNotification,
160161
object: nil,
161162
queue: nil,
162-
using: cloudStoreDidChange
163-
)
163+
using: { [weak self] notification in
164+
self?.cloudStoreDidChange(notification)
165+
})
164166
}
165167

166168
/// Handles cloud store changes from external sources.
@@ -189,7 +191,9 @@ final class CloudObserveFirstFixture {
189191
}
190192

191193
deinit {
192-
NotificationCenter.default.removeObserver(self)
194+
if let observer = notificationObserver {
195+
NotificationCenter.default.removeObserver(observer)
196+
}
193197
}
194198
}
195199

Tests/ObservableDefaultsMacroTests/__Snapshots__/ObservableCloudSyncImmediately.expanded.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ final class CloudSyncImmediatelyFixture {
116116
private final class CloudObservation: @unchecked Sendable {
117117
weak var host: CloudSyncImmediatelyFixture?
118118
let prefix: String
119+
private var notificationObserver: NSObjectProtocol?
119120

120121
/// Initializes the observation with the specified parameters.
121122
/// - Parameters:
@@ -124,13 +125,14 @@ final class CloudSyncImmediatelyFixture {
124125
init(host: CloudSyncImmediatelyFixture, prefix: String) {
125126
self.host = host
126127
self.prefix = prefix
127-
NotificationCenter.default
128+
notificationObserver = NotificationCenter.default
128129
.addObserver(
129130
forName: NSUbiquitousKeyValueStore.didChangeExternallyNotification,
130131
object: nil,
131132
queue: nil,
132-
using: cloudStoreDidChange
133-
)
133+
using: { [weak self] notification in
134+
self?.cloudStoreDidChange(notification)
135+
})
134136
}
135137

136138
/// Handles cloud store changes from external sources.
@@ -155,7 +157,9 @@ final class CloudSyncImmediatelyFixture {
155157
}
156158

157159
deinit {
158-
NotificationCenter.default.removeObserver(self)
160+
if let observer = notificationObserver {
161+
NotificationCenter.default.removeObserver(observer)
162+
}
159163
}
160164
}
161165

Tests/ObservableDefaultsMacroTests/__Snapshots__/ObservableCloudSyncImmediately.formatted.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ final class CloudSyncImmediatelyFixture {
116116
private final class CloudObservation: @unchecked Sendable {
117117
weak var host: CloudSyncImmediatelyFixture?
118118
let prefix: String
119+
private var notificationObserver: NSObjectProtocol?
119120

120121
/// Initializes the observation with the specified parameters.
121122
/// - Parameters:
@@ -124,13 +125,14 @@ final class CloudSyncImmediatelyFixture {
124125
init(host: CloudSyncImmediatelyFixture, prefix: String) {
125126
self.host = host
126127
self.prefix = prefix
127-
NotificationCenter.default
128+
notificationObserver = NotificationCenter.default
128129
.addObserver(
129130
forName: NSUbiquitousKeyValueStore.didChangeExternallyNotification,
130131
object: nil,
131132
queue: nil,
132-
using: cloudStoreDidChange
133-
)
133+
using: { [weak self] notification in
134+
self?.cloudStoreDidChange(notification)
135+
})
134136
}
135137

136138
/// Handles cloud store changes from external sources.
@@ -159,7 +161,9 @@ final class CloudSyncImmediatelyFixture {
159161
}
160162

161163
deinit {
162-
NotificationCenter.default.removeObserver(self)
164+
if let observer = notificationObserver {
165+
NotificationCenter.default.removeObserver(observer)
166+
}
163167
}
164168
}
165169

Tests/ObservableDefaultsMacroTests/__Snapshots__/ObservableDefaultsBasic.expanded.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ final class DefaultsBasicFixture {
123123
let userDefaults: Foundation.UserDefaults
124124
let prefix: String
125125
let observableKeysBlacklist: [String]
126+
private var notificationObserver: NSObjectProtocol?
126127

127128
/// Initializes the observation with the specified parameters.
128129
/// - Parameters:
@@ -136,13 +137,14 @@ final class DefaultsBasicFixture {
136137
self.prefix = prefix
137138
self.observableKeysBlacklist = observableKeysBlacklist
138139

139-
NotificationCenter.default
140+
notificationObserver = NotificationCenter.default
140141
.addObserver(
141142
forName: UserDefaults.didChangeNotification,
142143
object: userDefaults,
143144
queue: nil,
144-
using: userDefaultsDidChange
145-
)
145+
using: { [weak self] notification in
146+
self?.userDefaultsDidChange(notification)
147+
})
146148
}
147149

148150
/// Handles UserDefaults changes from external sources.
@@ -180,7 +182,9 @@ final class DefaultsBasicFixture {
180182
}
181183

182184
deinit {
183-
NotificationCenter.default.removeObserver(self)
185+
if let observer = notificationObserver {
186+
NotificationCenter.default.removeObserver(observer)
187+
}
184188
}
185189
}
186190

Tests/ObservableDefaultsMacroTests/__Snapshots__/ObservableDefaultsBasic.formatted.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ final class DefaultsBasicFixture {
123123
let userDefaults: Foundation.UserDefaults
124124
let prefix: String
125125
let observableKeysBlacklist: [String]
126+
private var notificationObserver: NSObjectProtocol?
126127

127128
/// Initializes the observation with the specified parameters.
128129
/// - Parameters:
@@ -136,13 +137,14 @@ final class DefaultsBasicFixture {
136137
self.prefix = prefix
137138
self.observableKeysBlacklist = observableKeysBlacklist
138139

139-
NotificationCenter.default
140+
notificationObserver = NotificationCenter.default
140141
.addObserver(
141142
forName: UserDefaults.didChangeNotification,
142143
object: userDefaults,
143144
queue: nil,
144-
using: userDefaultsDidChange
145-
)
145+
using: { [weak self] notification in
146+
self?.userDefaultsDidChange(notification)
147+
})
146148
}
147149

148150
/// Handles UserDefaults changes from external sources.
@@ -184,7 +186,9 @@ final class DefaultsBasicFixture {
184186
}
185187

186188
deinit {
187-
NotificationCenter.default.removeObserver(self)
189+
if let observer = notificationObserver {
190+
NotificationCenter.default.removeObserver(observer)
191+
}
188192
}
189193
}
190194

Tests/ObservableDefaultsMacroTests/__Snapshots__/ObservableDefaultsObserveFirst.expanded.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ final class DefaultsObserveFirstFixture {
122122
let userDefaults: Foundation.UserDefaults
123123
let prefix: String
124124
let observableKeysBlacklist: [String]
125+
private var notificationObserver: NSObjectProtocol?
125126

126127
/// Initializes the observation with the specified parameters.
127128
/// - Parameters:
@@ -135,13 +136,14 @@ final class DefaultsObserveFirstFixture {
135136
self.prefix = prefix
136137
self.observableKeysBlacklist = observableKeysBlacklist
137138

138-
NotificationCenter.default
139+
notificationObserver = NotificationCenter.default
139140
.addObserver(
140141
forName: UserDefaults.didChangeNotification,
141142
object: userDefaults,
142143
queue: nil,
143-
using: userDefaultsDidChange
144-
)
144+
using: { [weak self] notification in
145+
self?.userDefaultsDidChange(notification)
146+
})
145147
}
146148

147149
/// Handles UserDefaults changes from external sources.
@@ -173,7 +175,9 @@ final class DefaultsObserveFirstFixture {
173175
}
174176

175177
deinit {
176-
NotificationCenter.default.removeObserver(self)
178+
if let observer = notificationObserver {
179+
NotificationCenter.default.removeObserver(observer)
180+
}
177181
}
178182
}
179183

Tests/ObservableDefaultsMacroTests/__Snapshots__/ObservableDefaultsObserveFirst.formatted.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ final class DefaultsObserveFirstFixture {
122122
let userDefaults: Foundation.UserDefaults
123123
let prefix: String
124124
let observableKeysBlacklist: [String]
125+
private var notificationObserver: NSObjectProtocol?
125126

126127
/// Initializes the observation with the specified parameters.
127128
/// - Parameters:
@@ -135,13 +136,14 @@ final class DefaultsObserveFirstFixture {
135136
self.prefix = prefix
136137
self.observableKeysBlacklist = observableKeysBlacklist
137138

138-
NotificationCenter.default
139+
notificationObserver = NotificationCenter.default
139140
.addObserver(
140141
forName: UserDefaults.didChangeNotification,
141142
object: userDefaults,
142143
queue: nil,
143-
using: userDefaultsDidChange
144-
)
144+
using: { [weak self] notification in
145+
self?.userDefaultsDidChange(notification)
146+
})
145147
}
146148

147149
/// Handles UserDefaults changes from external sources.
@@ -176,7 +178,9 @@ final class DefaultsObserveFirstFixture {
176178
}
177179

178180
deinit {
179-
NotificationCenter.default.removeObserver(self)
181+
if let observer = notificationObserver {
182+
NotificationCenter.default.removeObserver(observer)
183+
}
180184
}
181185
}
182186

0 commit comments

Comments
 (0)