Observing External Changes to the Calendar Database

It’s possible for another process or app to modify the Calendar database while your app is running. If your app fetches calendar events or reminders, you should register to be notified about changes to the Calendar database. By doing so, you ensure that the calendar and reminder information you display to the user is current.

Registering for Notifications

An EKEventStore object posts an EKEventStoreChangedNotification notification whenever it detects changes to the Calendar database. Register for this notification if your app handles event or reminder data.

The following code registers for the EKEventStoreChangedNotification notification, as shown in Listing 5-1.

Listing 5-1  The EKEventStoreChangedNotification notification

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(storeChanged:)
                                             name:EKEventStoreChangedNotification
                                           object:eventStore];

Responding to Notifications

When you receive an EKEventStoreChangedNotification notification, it’s possible that objects you’ve fetched—such as an EKEvent, EKReminder, or EKCalendar, among others—have changed. The effect of these changes depends on whether an event was added, modified, or deleted.

Because your local data is often invalidated or incomplete when a change occurs in the Calendar database, you should refetch your current date range of events whenever you receive an EKEventStoreChangedNotification notification. If you are currently modifying an event and you do not want to refetch it unless it is absolutely necessary to do so, you can call the refresh method on the event. If the method returns YES, you can continue to use the event; otherwise, you need to refetch it.