gpt4 book ai didi

ios - 带有 EKEventStoreChanged 通知的 NotificationCenter 使用不同的通知多次调用

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:56:06 25 4
gpt4 key购买 nike

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

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

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

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}}"

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

最佳答案

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

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

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

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com