我正在应用程序中使用EKEventStore。当对日历进行更改时,我获取默认的存储并注册EKEventStoreChangedNotification,以便得到通知。但是,当进行更改时,通知的发送方会被多次(5-10)次调用,有时在每次调用之间最多需要15秒。这会使我的代码混乱,并使工作变得更加困难。我能做些什么吗?
谢谢
iOS7编辑:似乎在发布iOS7时,这个问题已经消失了。现在,不管对CalendarStore所做的更改如何,只发送一个EKEventStoreChangedNotification。
发布于 2012-08-30 21:22:20
这是一个确切的结果,即通知是如何发送的,以及每个通知实际上是如何通知的。根据我的经验,您至少可以收到一份更改项目(事件、提醒等)的通知。并至少为该项的包含日历的结果更改多一个。
如果没有看到您的代码,并且不知道正在做什么更改,我就不能对一个答案太具体;但是,一般来说,您有两个选项。
后一种解决方案是我喜欢的答案,它看起来可能类似于(暂时忽略线程问题):
@property (strong) NSTimer *handlerTimer;
- (void)handleNotification:(NSNotification *)note {
// This is the function that gets called on EKEventStoreChangedNotifications
[self.handlerTimer invalidate];
self.handlerTimer = [NSTimer timerWithTimeInterval:2.0
target:self
selector:@selector(respond)
userInfo:nil
repeats:NO];
[[NSRunLoop mainRunLoop] addTimer:self.handlerTimer
forMode:NSDefaultRunLoopMode];
}
- (void)respond {
[self.handlerTimer invalidate];
NSLog(@"Here's where you actually respond to the event changes");
}发布于 2018-09-03 16:02:24
我也有这个问题。经过一些调试后,我意识到我导致了额外的EKEventStoreChangedNotification调用(例如,通过创建或删除EKReminders)。当我执行一个EKEventStoreChangedNotification时,最终会触发estore.commit()。
我通过声明全局变量来修正这个问题,如下所示:
// the estore.commit in another function causes a new EKEventStoreChangedNotification. In such cases I will set the ignoreEKEventStoreChangedNotification to true so that I DON'T trigger AGAIN some of the other actions.
var ignoreEKEventStoreChangedNotification = false 然后在AppDelegate.swift中做这件事,我在那里听EKEventStoreChangedNotification
nc.addObserver(forName: NSNotification.Name(rawValue: "EKEventStoreChangedNotification"), object: estore, queue: updateQueue) {
notification in
if ignoreEKEventStoreChangedNotification {
ignoreEKEventStoreChangedNotification = false
return
}
// Rest of your code here.
}在我更改存储库的函数中,我这样做:
//
// lots of changes to estore here...
//
ignoreEKEventStoreChangedNotification = true // the estore.commit causes a new EKEventStoreChangedNotification because I made changes. Ignore this EKEventStoreChangedNotification - I know that changes happened, I caused them!
try estore.commit()它不适合全局变量(特别是如果您喜欢函数式编程),但它可以工作。
https://stackoverflow.com/questions/12205484
复制相似问题