I've experimentally seen that the notifications(named:) API of NotificationCenter appears to buffer observed notifications internally. In local testing it appears to be limited to 8 messages. I've been unable to find any documentation of this fact, and the behavior seems like it could lead to software bugs if code is not expecting notifications to potentially be dropped. Is this behavior expected and documented somewhere?
Here is a sample program demonstrating the behavioral difference between the Combine and AsyncSequence-based notification observations:
@Test
nonisolated func testNotificationRace() async throws {
let testName = Notification.Name("TestNotification")
let notificationCount = 100
var observedAsyncIDs = [Int]()
var observedCombineIDs = [Int]()
let subscribe = Task { @MainActor in
print("setting up observer...")
let token = NotificationCenter.default.publisher(for: testName)
.sink { value in
let id = value.userInfo?["id"] as! Int
observedCombineIDs.append(id)
print("🚜 observed note with id: \(id)")
}
defer { extendLifetime(token) }
for await note in NotificationCenter.default.notifications(named: testName) {
let id: Int = note.userInfo?["id"] as! Int
print("🚰 observed note with id: \(id)")
observedAsyncIDs.append(id)
if id == notificationCount { break }
}
}
let post = Task { @MainActor in
for i in 1...notificationCount {
NotificationCenter.default.post(
name: testName,
object: nil,
userInfo: ["id": i]
)
}
}
_ = await (post.value, subscribe.value)
#expect(observedAsyncIDs.count == notificationCount) // 🛑 Expectation failed: (observedAsyncIDs.count → 8) == (notificationCount → 100)
#expect(observedCombineIDs == Array(1...notificationCount))
print("done")
}
it appears to be limited to 8 messages.
Yep. AFAICT this is simply hard coded )-:
Is this behavior expected and documented somewhere?
Not that I’ve seen. I recommend that you file a bug about this, either requesting that it be removed, or that it be documented, or both (-:
Please post your bug number, or numbers, just for the record.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"