我有一个 iOS 小部件,它使用 EKEventStore
来处理提醒。小部件仅初始化 EKEventStore 一次,然后使用该实例。小部件通常在当今的 viewController 中查看时创建,并在用户退出通知中心时销毁。这会导致每次用户查看小部件时都会初始化 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) {
}];
我知道在小部件的同一 session 中多次初始化 EKEventStore
可能会出现问题。但是,当用户离开小部件时,我预计初始化 EKEventStore 的有限次数会在小部件从头开始重新加载时重置。
最佳答案
实现一个共享单例类来管理EKEventStore
,如下所示,在应用程序和扩展(小部件)中使用单例:
+(EventStoreManager *)sharedInstance {
static dispatch_once_t onceToken;
static EventStoreManager * eventStoreSharedInstance;
dispatch_once(&onceToken, ^{
eventStoreSharedInstance = [[EventStoreManager alloc] init];
});
return eventStoreSharedInstance;
}
这解决了上述问题。即使扩展或小部件被销毁,这个单例也会被维护。 感谢 Apple 支持提供此修复程序。
关于ios - EKEventStore 与 iOS Widget 错误 : [EventKit] Client tried to open too many connections to calaccessd. 拒绝打开另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58160810/