首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >EKEventStoreChangedNotification被多次调用

EKEventStoreChangedNotification被多次调用
EN

Stack Overflow用户
提问于 2012-08-30 21:04:01
回答 2查看 2K关注 0票数 8

我正在应用程序中使用EKEventStore。当对日历进行更改时,我获取默认的存储并注册EKEventStoreChangedNotification,以便得到通知。但是,当进行更改时,通知的发送方会被多次(5-10)次调用,有时在每次调用之间最多需要15秒。这会使我的代码混乱,并使工作变得更加困难。我能做些什么吗?

谢谢

iOS7编辑:似乎在发布iOS7时,这个问题已经消失了。现在,不管对CalendarStore所做的更改如何,只发送一个EKEventStoreChangedNotification

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-08-30 21:22:20

这是一个确切的结果,即通知是如何发送的,以及每个通知实际上是如何通知的。根据我的经验,您至少可以收到一份更改项目(事件、提醒等)的通知。并至少为该项的包含日历的结果更改多一个。

如果没有看到您的代码,并且不知道正在做什么更改,我就不能对一个答案太具体;但是,一般来说,您有两个选项。

  1. 更仔细地观察这些更改--如果这些更改涉及到与应用程序无关的事件,或者您已经处理了某个特定项的更改,那么您可以安全地忽略一些通知。
  2. 将多个更改合并为一批处理程序代码。基本上,当您收到通知时,而不是立即启动响应,而是启动一个计时器,它将在一两秒钟内运行响应。然后,如果在计时器触发之前出现了另一个通知,则可以取消定时器并重置它。通过这种方式,您可以批量处理短时间窗口中出现的多个通知,并且只响应它们一次(当计时器最终触发时)。

后一种解决方案是我喜欢的答案,它看起来可能类似于(暂时忽略线程问题):

代码语言:javascript
复制
@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");
}
票数 17
EN

Stack Overflow用户

发布于 2018-09-03 16:02:24

我也有这个问题。经过一些调试后,我意识到我导致了额外的EKEventStoreChangedNotification调用(例如,通过创建或删除EKReminders)。当我执行一个EKEventStoreChangedNotification时,最终会触发estore.commit()

我通过声明全局变量来修正这个问题,如下所示:

代码语言:javascript
复制
// 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

代码语言:javascript
复制
nc.addObserver(forName: NSNotification.Name(rawValue: "EKEventStoreChangedNotification"), object: estore, queue: updateQueue) {
            notification in

        if ignoreEKEventStoreChangedNotification {
            ignoreEKEventStoreChangedNotification = false
            return
        }

        // Rest of your code here.

}

在我更改存储库的函数中,我这样做:

代码语言:javascript
复制
//
// 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()

它不适合全局变量(特别是如果您喜欢函数式编程),但它可以工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12205484

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档