【问题标题】:NotificationCenter with EKEventStoreChanged notification called multiple times with different notifications带有 EKEventStoreChanged 通知的 NotificationCenter 使用不同的通知多次调用
【发布时间】:2016-11-16 23:18:36
【问题描述】:

首先请注意,我没有像其他各种问题中所述那样多次注册为观察员。

当我在本机日历应用程序中更改某些内容并返回到我的应用程序时,选择器会被调用 4-5 次并发出不同的通知。

viewDidLoad 中,我删除了所有可能的观察者并注册了一次 agian:

NotificationCenter.default.removeObserver(self)
NotificationCenter.default.addObserver(self, selector: #selector(reloadModelData(notification:)), name: Notification.Name.EKEventStoreChanged, object: nil)

对应的方法:

@objc private func reloadModelData(notification: NSNotification) {
    debugPrint("method called \(notification)")
}

导致这个输出

"方法称为 NSConcreteNotification 0x170246300 {name = EKEventStoreChangedNotification; object = ; userInfo = {\n EKEventStoreChangedObjectIDsUserInfoKey = (\n \"x-apple-eventkit:///Location/p259707\",\n \"x-apple -eventkit:///Event/p264955\"\n);\n}}" "方法称为 NSConcreteNotification 0x174258840 {name = EKEventStoreChangedNotification; object = ; userInfo = {\n EKEventStoreChangedObjectIDsUserInfoKey = (\n \"x-apple-eventkit:///Location/p259707\",\n \"x-apple-eventkit: ///事件/p264955\"\n);\n}}" "方法称为 NSConcreteNotification 0x17024b250 {name = EKEventStoreChangedNotification; object = ; userInfo = {\n EKEventStoreChangedObjectIDsUserInfoKey = (\n \"x-apple-eventkit:///Location/p259707\",\n \"x-apple-eventkit: ///事件/p264955\"\n);\n}}" "方法称为 NSConcreteNotification 0x174253b00 {name = EKEventStoreChangedNotification; object = ; userInfo = {\n EKEventStoreChangedObjectIDsUserInfoKey = (\n \"x-apple-eventkit:///Location/p259707\",\n \"x-apple-eventkit: ///事件/p264955\"\n );\n}}"

有谁知道如何解决这个问题。意思是在重新进入应用时只收到该观察者的一次调用?

【问题讨论】:

  • 请说明您遇到的问题。当你征求建议时,不清楚你的意思是什么。
  • 我相应地编辑了问题
  • 您找到解决方案了吗?我有同样的问题

标签: ios swift nsnotificationcenter


【解决方案1】:

我猜,这就是它的工作原理,日历会向您发送有关您所做的所有原始更改的通知。

The Apple's documentation 建议致电refresh 检查您正在访问的提醒和事件。如果它返回true,则无需重新获取它们。所以多个通知到达不会造成太大问题。

我还建议在viewWillAppear(_:) 订阅通知并在viewDidDisappear(_:) 取消订阅。当然可能会有例外,但通常你不想在屏幕不活动时处理它们。

【讨论】:

  • 根据文档,重新获取所有事件和提醒也是可以的——这就是我要做的。即使我只是删除一个事件,也会有 4-5 次调用。在我看来,这不应该触发这么多观察者行动。
  • @ph1lb4 那么有什么问题呢?
  • @ph1lb4 尝试在viewDidDisappear 中添加removeObserver 并添加viewWillAppear。也会出现在视图中,您必须检查是否有任何更改。
  • 感谢您的提示,不幸的是我已经尝试过了。问题不在于我多次订阅观察者,因为每次通知都不同。问题是只有一个应该被触发时,有几个通知。
  • @ph1lb4 我没有说您订阅了多次。我说过只有在视图控制器可见时才应该订阅。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-31
  • 2014-06-06
  • 1970-01-01
  • 1970-01-01
  • 2018-05-29
相关资源
最近更新 更多