Maintain sync check link for calendar

For my application, I need to create a Calendar in iCloud user account and add events to this calendar.

I am creating a calendar using this:

let newCalendar = EKCalendar(forEntityType: EKEntityTypeEvent, eventStore: EventManager.sharedInstance.eventStore) newCalendar.title = "MyAppCalendar" let color = UIColor(red: CGFloat(arc4random_uniform(255))/255, green: CGFloat(arc4random_uniform(255))/255, blue: CGFloat(arc4random_uniform(255))/255, alpha: 1) newCalendar.CGColor = color.CGColor var mySource:EKSource? for source in EventManager.sharedInstance.eventStore?.sources() as! [EKSource] { if source.sourceType.value == EKSourceTypeCalDAV.value && source.title == "iCloud" { mySource = source break } } if mySource == nil { //crearting alert and displaying to user return } else { newCalendar.source = mySource! } var error:NSError? if EventManager.sharedInstance.eventStore!.saveCalendar(newCalendar, commit: true, error: &error) { let calendarName = newCalendar.title let calendarIdentifier = newCalendar.calendarIdentifier //save these in db and server }else { SharedConstants.handleErrors(error!) } 

Where EventManager is my class to support a reference to an instance of an EKEventStore object.

But Apple's documentation says that calendarIdentifier changes during synchronization.

So my question is how to maintain a link to this calendar?

+4
source share
1 answer

There is no solution that will work in 100% of cases, because, as you know, calendarIdentifier can be changed. Thus, there is no unique identifier that will refer to a particular calendar all the time.
But you can make a decision that will work in most cases. I suggest using such an algorithm to access a specific calendar:
I assume that you have cached calendarIdentifier and title for your calendar.

  • Iterate over all calendars.
  • Check if the calendar has an identifier equal to the cached identifier.
  • If the identifiers are equal, then you have found your calendar.
  • Otherwise, check if any calendar has a title equal to the cached identifier.
  • If the headings are equal, you may have found your calendar. This may not be true if the user has replaced the headers for his calendar and another. I think this scenario has a pretty low chance.
  • Otherwise, you cannot find your calendar. This can happen if the user deletes your calendar.

Here is the code snippet for the above algorithm:

 func eventsCalendarWithIdentifier(identifier: String!, title: String!) -> EKCalendar? { var calendarWithIdentifier: EKCalendar? = nil var calendarWithTitle: EKCalendar? = nil let eventStore = EventManager.sharedInstance.eventStore! for aCalendar in eventStore.calendarsForEntityType(EKEntityTypeEvent) as! [EKCalendar] { if var anIdentifier = aCalendar.calendarIdentifier { if anIdentifier == identifier { calendarWithIdentifier = aCalendar break } } if var aTitle = aCalendar.title { if aTitle == title { calendarWithTitle = aCalendar } } } return calendarWithIdentifier ?? calendarWithTitle } 

It is also important to handle events when full synchronization occurs and recreate an instance of your calendar. This is important because the user can delete his calendar while your application is in the background, in which case the calendar will be invalid.
Here is a code snippet to handle such events:

 func registerObserver() { NSNotificationCenter.defaultCenter().addObserver(self, selector: "storeChanged", name: EKEventStoreChangedNotification, object: nil) } func unregisterObserver() { NSNotificationCenter.defaultCenter().removeObserver(self, name: EKEventStoreChangedNotification, object: nil) } func storeChanged() { let myCalendar = self.eventsCalendarWithIdentifier(cachedIdentifier, title: cachedTitle) if myCalendar == nil { // Calendar lost. } // Updated UI if needed. } 
+2
source

All Articles