NotificationCenter.notifications(named:) appears to buffer internally and can drop notifications, but is this documented anywhere?

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")
}
Answered by DTS Engineer in 867824022
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"

Accepted Answer
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"

FB21232935

Thanks!

ps It’s better to reply as a reply, rather than in the comments; see Quinn’s Top Ten DevForums Tips for this and other titbits.

This duplicate comment brought to you …

Indeed. Sorry about that. It’s a known issue with the comment system (i. 98940414)-:

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

NotificationCenter.notifications(named:) appears to buffer internally and can drop notifications, but is this documented anywhere?
 
 
Q