EKEventStore with iOS Widget 錯誤:[EventKit] 客戶端試開啟太多連接以 calac



我有一个使用EKEventStore来处理提醒的iOS小部件。小组件仅初始化一次EKEventStore,然后使用该实例。小部件通常在今天的视图控制器中查看时创建,并在用户退出通知中心时被销毁。这会导致每次用户查看小部件时初始化EKEventStore。连续查看小部件 10 次后,出现以下错误:

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

我诊断了问题,发现它发生在小部件的 10 次视图之后。要重现这种情况,您需要打开另一个应用程序,然后每次返回小部件,以便在查看小部件时重新加载。

我正确初始化EKEventStore如下:

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

我知道在小部件的同一会话中多次初始化EKEventStore可能是个问题。但是当用户离开小部件时,我希望初始化EKEventStore的有限次数在小部件从头开始重新加载时重置。

实现共享单例类来管理EKEventStore如下所示,在应用和扩展(小部件(中使用单例:

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

这解决了上述问题。即使扩展或小组件被销毁,也会保留此单一实例。 感谢苹果支持提供此修复程序。

最新更新