EKCalendar Not Persisting

I can create a new calendar and save it with the following function:

func createCalendarForUser() { let sourcesInEventStore = self.eventStore.sources / let subscribedSourceIndex = sourcesInEventStore.index {$0.title == "Subscribed Calendars"} if let subscribedSourceIndex = subscribedSourceIndex { let userCalendar = EKCalendar(for: .event, eventStore: self.eventStore) userCalendar.title = "newCalendar" userCalendar.source = sourcesInEventStore[subscribedSourceIndex] do { try self.eventStore.saveCalendar(userCalendar, commit: true) print("calendar creation successful") } catch { print("cal \(userCalendar.source.title) failed : \(error)") } } }


This functions great while the app is open and running. I can save events to them, i can see them in my local calendar, and life is good. However, once the app is terminated and goes into the background the calendars disappear, along with any events created in them. I've tried saving the calendar to different sources other then the

Subscribed Calendars
source but when i do that the calendars wont even save in the first place. Heres one of the attempts at using the local source:
func createCalendarForUser() { let sourcesInEventStore = self.eventStore.sources / let localSourceIndex = sourcesInEventStore.index {$0.sourceType == .local} if let localSourceIndex = localSourceIndex { let userCalendar = EKCalendar(for: .event, eventStore: self.eventStore) userCalendar.title = "newCalendar" userCalendar.source = sourcesInEventStore[localSourceIndex] do { try self.eventStore.saveCalendar(userCalendar, commit: true) print("creating new calendar successful") } catch { print("creating new calendar failed : \(error)") } } }


I also tried this method :

func createCalendarForUser() { let sourcesInEventStore = self.eventStore.sources / let userCalendar = EKCalendar(for: .event, eventStore: self.eventStore) userCalendar.title = "newCalendar" userCalendar.source = sourcesInEventStore.filter{ (source: EKSource) -> Bool in source.sourceType.rawValue == EKSourceType.local.rawValue }.first! do { try self.eventStore.saveCalendar(userCalendar, commit: true) print("creating new calendar succesful") } catch { print("creating new calendar failed : \(error)") } }


As metioned here as mentioned here https://www.andrewcbancroft.com/2015/06/17/creating-calendars-with-event-kit-and-swift/

Has anyone else come across this problem?

Replies

We were seeing something similar to this. The new calendar did persist sometimes, but other times our users would lose the calendar and all events entered in it. It was not easily/reliably reproducible.
In the end, we decided to just direct users to the iOS Calendar app to create new calendars, but this is far from ideal.
The fact that the iOS Calendar app can do it reliably, and we couldn't however does seem to imply that we're doing something wrong, because both probably use the same API to create the calendars (of course this is not certain because Apple's apps do have access to much more functionality).

Did you come up with a solution?