Skip to content

Commit 2134642

Browse files
committed
Fire the notification unconditionally, and check that it is posted in the test.
1 parent fae057c commit 2134642

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

Foundation/UserDefaults.swift

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -359,39 +359,29 @@ open class UserDefaults: NSObject {
359359

360360
open func setPersistentDomain(_ domain: [String : Any], forName domainName: String) {
361361
if let defaults = UserDefaults(suiteName: domainName) {
362-
var removedAny = false
363362
for key in defaults._dictionaryRepresentation(searchingOutsideOfSuite: false).keys {
364363
defaults.removeObject(forKey: key)
365-
removedAny = true
366364
}
367365

368-
var addedAny = false
369366
for (key, value) in domain {
370367
defaults.set(value, forKey: key)
371-
addedAny = true
372368
}
373369

374370
_ = defaults.synchronize()
375371

376-
if removedAny || addedAny {
377-
NotificationCenter.default.post(name: UserDefaults.didChangeNotification, object: self)
378-
}
372+
NotificationCenter.default.post(name: UserDefaults.didChangeNotification, object: self)
379373
}
380374
}
381375

382376
open func removePersistentDomain(forName domainName: String) {
383377
if let defaults = UserDefaults(suiteName: domainName) {
384-
var removedAny = false
385378
for key in defaults._dictionaryRepresentation(searchingOutsideOfSuite: false).keys {
386379
defaults.removeObject(forKey: key)
387-
removedAny = true
388380
}
389381

390382
_ = defaults.synchronize()
391383

392-
if removedAny {
393-
NotificationCenter.default.post(name: UserDefaults.didChangeNotification, object: self)
394-
}
384+
NotificationCenter.default.post(name: UserDefaults.didChangeNotification, object: self)
395385
}
396386
}
397387

TestFoundation/TestUserDefaults.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,21 @@ class TestUserDefaults : XCTestCase {
299299
let dictionary = (key: "A Dictionary", value: [ "Swift": "Imperative", "Haskell": "Functional", "LISP": "LISP", "Today": Date() ] as [String: AnyHashable])
300300

301301
let domainName = "org.swift.Foundation.TestPersistentDomainName"
302+
303+
let done = expectation(description: "All notifications have fired.")
304+
305+
var countOfFiredNotifications = 0
306+
let expectedNotificationCount = 3
307+
308+
let observer = NotificationCenter.default.addObserver(forName: UserDefaults.didChangeNotification, object: nil, queue: .main) { (_) in
309+
countOfFiredNotifications += 1
310+
311+
if countOfFiredNotifications == expectedNotificationCount {
312+
done.fulfill()
313+
} else if countOfFiredNotifications > expectedNotificationCount {
314+
XCTFail("Too many UserDefaults.didChangeNotification notifications posted.")
315+
}
316+
}
302317

303318
let defaults1 = UserDefaults(suiteName: nil)!
304319

@@ -335,5 +350,9 @@ class TestUserDefaults : XCTestCase {
335350
if let domain = defaults3.persistentDomain(forName: domainName) {
336351
XCTAssertEqual(domain.count, 0)
337352
} // else it's nil, which is also OK.
353+
354+
waitForExpectations(timeout: 10)
355+
356+
NotificationCenter.default.removeObserver(observer)
338357
}
339358
}

0 commit comments

Comments
 (0)