What exactly constitutes a “full calendar sync” in EKCalendar?

青春壹個敷衍的年華 提交于 2019-12-03 01:29:46

When exactly does a "full sync" occur?
Calendar and Reminders Programming Guide explains this questions in this way:

If a change to the Calendar database occurs from outside of your app, Event Kit is able to detect the change by notification so your app can act appropriately. Changes made to calendar items with Event Kit are automatically synced to the associated calendar (CalDAV, Exchange, and so on).

I see such scenarios of "full sync" events while your app is open:
1. User sends your app to background and opens Calendar app. He changes calendar name, adds/edits/deletes events or even removes some calendar.
2. User applies some changes to iCloud calendar on Mac. iOS device is notified that iCloud calendar was changed so it has to be synchronized.
3. Third-party app receives silent notification, iOS launches it in background, app creates some calendar event based on notification.

In general it means that "full sync" event can happen at any time.

How to detect and handle "full sync" event?
Observing External Changes to the Calendar Database explains this questions in this way:

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.

Here is code sample for registering to such notification:

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

I think it makes sense to recreate instances of EKCalendar classes and recache calendarIdentifier if needed.

What properties are liable to change besides the calendarIdentifier?
I can't find any documentation about this question. But since calendar can even doesn't exist at some moment (for example user manually removes it in Calendar app) then any property of EKCalendar object can be invalid after "full sync" event happens.

Also it makes sense to read above links for more information and details.

Full sync happens when a calendar is added. All added calendars are cached on the system (iOS or MacOS) and receive a unique ID. This can be easily checked on MacOS - if you go to directory ~/Library/Calendars/ you will see a list with directories like:

3CC21C9A-0B3C-4A76-B2B0-8D3643CF2992.exchange/

45EF644F-672A-453A-ACC9-A565F017F766.calendar/

which are the unique IDs that can be checked with calendarIdentifier.

In order to test how calendarIdentifier changes on iOS you can create a calendar in iCloud with name testcal and use the following code to get the identifier:

EKEventStore *eventStore = [[EKEventStore alloc] init];
NSArray *cal = [eventStore calendarsForEntityType:EKEntityTypeEvent];
for (EKCalendar *i in cal) {
    if([i.title isEqualToString:@"testcal"]) {
        NSLog(@"%@", i.calendarIdentifier);
    }
}

After that, disable the calendar (Settings -> iCloud -> Calendars, choose "Delete from My iPhone") and enable it. When you execute the code again, you will see a different identifier, although the calendar is the same.

Another case where I see that a full sync will be executed is if the local cache is corrupted. In this case the Calendar should try to rebuild it.

So, it is not a good idea to find a calendar by its identifier and you can use title, type, color and etc. to identify it uniquely.

Based on iTunes forum, when it is full sync is not specified and depends on them:

http://www.openradar.appspot.com/15671424

https://idmsa.apple.com/IDMSWebAuth/login?appIdKey=4a75046cda87eab6386a9eae8caabb9824e328b9abc988119b39296495ec184c&path=/login.jspa#926856.

Related to the properties which are liable to change are all of them which can be accessed by other threads, (same than calendarIdentifier) hence the ones that are nonatomic and can be changed, here the ones that I could find:

allowsContentModifications, CGColor, immutable,title,type,allowedEntityTypes,source,subscribed,supportedEventAvailabilities
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!