swiftekeventstore

EKEventStore.calendars is always empty after clean install


I have a weird issue on my iOS app where, after a fresh install, if I try to add calendar events (after accepting the native calendar permissions prompt), my eventStore singleton never shows any available calendars, and defaultCalendarForNewEvents is always nil.

I've tried to debug it, and the odd part is that, if I try po EKEventStore().calendars(for: .event), then I can see them all (still on fresh install).

If I then kill the app and reopen it, my eventStore singleton suddenly starts working as expected.

I'm not sure if this behaviour has been documented before, it seems very strange to me and I couldn't find other posts with this specific issue...

Hereafter a snippet of my CalendarManager class and how the eventStore singleton is declared.

class CalendarManager {
    // MARK: Variables
    static let sharedInstance = CalendarManager()
    fileprivate init() { }

    let eventStore = EKEventStore()

    // further business logic...
}

And further down, the code where I encounter issues:

func createEvent(for workout: Workout, sendReminder: Bool = false) -> Bool {
    // If I add a breakpoint below and check a new instance of EKEventStore, 
    // I can see all calendars & defaultCalendarForNewEvents, 
    // however this `guard` never lets us through...
    guard let calendar = CalendarManager.sharedInstance.eventStore.defaultCalendarForNewEvents else { return false }

    // create event with default calendar...
}

What am I doing wrong?


Solution

  • Make sure you aren't initializing EKEventStore before requesting calendar permissions.