ioswidgetios13ekeventstore

EKEventStore with iOS Widget error: [EventKit] Client tried to open too many connections to calaccessd. Refusing to open another


I have an iOS widget that uses EKEventStore to work with reminders. The widget only initializes the EKEventStore once then uses that instance. Widgets usually get created as they are viewed in the today's viewController and get destroyed when the user exits the notification center. This leads to initializing the EKEventStore every time the user views the widget. After 10 consecutive times of viewing the widget the following error occurs:

[EventKit] Client tried to open too many connections to calaccessd. Refusing to open another

I diagnosed the problem and found that it happens after 10 views of the widget. To reproduce this, you need to open another app then come back to the widget every time in order for the widget to reload when you view it.

I initialize the EKEventStore correctly as follows:

self.eventStore = [[EKEventStore alloc] init];
[self.eventStore requestAccessToEntityType:EKEntityTypeReminder
                                                   completion:^(BOOL granted, NSError *error) {

}];

I understand it might be a problem to initialize EKEventStore multiple times in the same session of the widget. But when the user goes away from the widget I expect this limited number of times of initializing the EKEventStore to reset when the widget reloads from the beginning.


Solution

  • Implement a shared singleton class to manage the EKEventStore as follows, use the singleton in both the app and the extension (widget):

    +(EventStoreManager *)sharedInstance {
        static dispatch_once_t onceToken;
        static EventStoreManager *  eventStoreSharedInstance;
    
        dispatch_once(&onceToken, ^{
            eventStoreSharedInstance = [[EventStoreManager alloc] init];
        });
        return eventStoreSharedInstance;
    }
    

    This solved the above issue. This singleton is maintained even when the extension or the widget is destroyed. Thanks to Apple support for providing this fix.