OGeek|极客世界-中国程序员成长平台

标题: ios - 带有 EKEventStoreChanged 通知的 NotificationCenter 使用不同的通知多次调用 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 18:04
标题: ios - 带有 EKEventStoreChanged 通知的 NotificationCenter 使用不同的通知多次调用

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

当我在 native 日历应用程序中更改某些内容并返回到我的应用程序时,选择器会被调用 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)")
}

导致这个输出

"method called NSConcreteNotification 0x170246300 {name = EKEventStoreChangedNotification; object = ; userInfo = {\n EKEventStoreChangedObjectIDsUserInfoKey = (\n \"x-apple-eventkit:///Location/p259707\",\n \"x-apple-eventkit:///Event/p264955\"\n );\n}}" "method called NSConcreteNotification 0x174258840 {name = EKEventStoreChangedNotification; object = ; userInfo = {\n EKEventStoreChangedObjectIDsUserInfoKey = (\n \"x-apple-eventkit:///Location/p259707\",\n \"x-apple-eventkit:///Event/p264955\"\n );\n}}" "method called NSConcreteNotification 0x17024b250 {name = EKEventStoreChangedNotification; object = ; userInfo = {\n EKEventStoreChangedObjectIDsUserInfoKey = (\n \"x-apple-eventkit:///Location/p259707\",\n \"x-apple-eventkit:///Event/p264955\"\n );\n}}" "method called NSConcreteNotification 0x174253b00 {name = EKEventStoreChangedNotification; object = ; userInfo = {\n EKEventStoreChangedObjectIDsUserInfoKey = (\n \"x-apple-eventkit:///Location/p259707\",\n \"x-apple-eventkit:///Event/p264955\"\n );\n}}"

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



Best Answer-推荐答案


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

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

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

关于ios - 带有 EKEventStoreChanged 通知的 NotificationCenter 使用不同的通知多次调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40613478/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4