How to observe calendar changes by using NotificationCenter.messages(of: for:)?

Overview

  • I would like to observe calendar changes using NotificationCenter.messages(of: for:)
  • I want receive Sendable messages, not traditional Notification which is not Sendable

Problem

  • I can't seem to get the following code to compile
import EventKit

NotificationCenter.default.messages(
    of: EKEventStore.EventStoreChanged.Subject.self,
    for: .changed
)

Reference

Questions

  • How can I use by using NotificationCenter.messages(of: for:) for Calendar changes?

Thanks for the post.

The NotificationCenter.messages(of:for:) API relies on frameworks providing strongly-typed, Sendable message structs. EventKit still relies on the traditional Notification.Name.EKEventStoreChanged.

Since your underlying goal is to observe calendar changes using an AsyncSequence of Sendable messages, you can use the standard .notifications(named:) API and immediately .map it to a custom Sendable struct?

While you cannot use NotificationCenter.messages(of:for:) for EventKit, using .notifications(named:).map { ... } achieves the exact same architectural goal: a concurrency-safe, Sendable asynchronous stream of calendar changes?

Albert  WWDR

Thanks @DTS Engineer Albert, however my scenario is a bit more complex.

My scenario

  • I would like to debounce (AsyncAlgorithms) on the notifications
  • However that will not compile using .notifications(named:)
  • It will throw the error Conformance of 'Notification' to 'Sendable' is unavailable in iOS

Code that doesn't compile

import EventKit
import AsyncAlgorithms

func f1() async {
    let center = NotificationCenter.default
    let notifications = center.notifications(named: .EKEventStoreChanged)

    // Error: Conformance of 'Notification' to 'Sendable' is unavailable in iOS
    for await notification in notifications.debounce(for: .seconds(2)) {
        // Do something
    }
}

My understanding

  • So in this case I would need to use messages(of:for:) to be able to debounce
  • This is the reason why messages(of:for:) exists to cater to such scenarios.

Problem

Questions

  • Since .changed is available on the API as an option for EventKit store changes (refer documentation link above), is this an unfinished implementation / bug from Foundation / EventKit framework?
  • Can you please fix it or provide a workaround?
How to observe calendar changes by using NotificationCenter.messages(of: for:)?
 
 
Q