简体   繁体   English

带有EKEventStoreChanged通知的NotificationCenter多次调用了不同的通知

[英]NotificationCenter with EKEventStoreChanged notification called multiple times with different notifications

First of all please note that I am not registered multiple times as observer as described in various other questions. 首先请注意,我没有像其他各种问题所述多次注册为观察员。

When I change something in the native calendar application and return back to my app the selector gets called 4-5 times with different notifications. 当我在本机日历应用程序中更改某些内容并返回到我的应用程序时,选择器被调用4-5次,并带有不同的通知。

In viewDidLoad I remove any possible observers and register agian once: viewDidLoad我删除所有可能的观察者并注册agian一次:

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

The corresponding method: 对应的方法:

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

Leads to this output 导致此输出

"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/ “称为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:/// Event / p264955 \\“ \\ n}; \\ n}}”“”称为NSConcreteNotification 0x17024b250的方法{name = EKEventStoreChangedNotification; object =; userInfo = { \\ n EKEventStoreChangedObjectIDsUserInfoKey =(\\ n \\“ x-apple-eventkit:/// Location / p259707 \\”,\\ n \\“ x-apple-eventkit:/// Event / p264955 \\” \\ n); \\ n}} “”称为NSConcreteNotification 0x174253b00的方法{name = EKEventStoreChangedNotification; object =; userInfo = {\\ n EKEventStoreChangedObjectIDsUserInfoKey =(\\ n \\“ x-apple-eventkit:/// Location / p259707 \\”,\\ n \\“ x-apple-eventkit :///事件/ p264955\\"\\n );\\n}}" p264955 \\“ \\ n); \\ n}}”

Does anyone know how fix this. 有谁知道如何解决这个问题。 Meaning only receiving one single call of this observer when re-entering the app? 意味着重新进入应用程序时仅收到该观察者的一个呼叫?

I guess, this is how it works, the calendar sends you notifications for all primitive changes you've made. 我想这就是它的工作原理,日历会向您发送有关您所做的所有原始更改的通知。

The Apple's documentation suggests to check reminders and events you're accessing by calling refresh . Apple的文档建议通过调用refresh检查您正在访问的提醒和事件。 If it returns true , there's no need to refetch them. 如果返回true ,则无需重新获取它们。 So multiple notifications arriving wouldn't cause much of a problem. 因此,多个通知到达不会造成太大的问题。

Also I would recommend to subscribe for notifications in viewWillAppear(_:) and unsubscribe in viewDidDisappear(_:) . 我也建议您在viewWillAppear(_:)订阅通知,并在viewDidDisappear(_:)退订。 Of course there might be exceptions, but usually you don't want to handle them when the screen is not active. 当然可能会有例外,但是通常您不希望在屏幕不活动时处理它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NotificationCenter:如何在 BaseController 中添加通知而不添加多次? - NotificationCenter: How to add a notification in BaseController without it being added multiple times? NotificationCenter.default.addObserver 使用 Unwind Segue 不断被调用多次 - NotificationCenter.default.addObserver keep getting called multiple times with Unwind Segue 即使观察者被移除,通知观察者也会被多次调用 - Notification observer called multiple times even though the observer is removed 如何使用在不同时间到达的用户数据处理多个推送通知? - How to handle multiple push notifications with user data arrived at different times? NotificationCenter注册的选择器未调用 - NotificationCenter registered selector is not called 通知中心观察者未在 swift 4 中调用 - NotificationCenter obersever not called in swift 4 如何检索EKEventStoreChanged通知中已更改的事件 - How to retrieve events that has been changed in EKEventStoreChanged notification 检测iOS上的传入NotificationCenter通知 - Detect incoming NotificationCenter notification on iOS 在Swift中设置多次通知 - Setting Multiple Times for Notifications in Swift 在CollectionViewCell中未调用的NotificationCenter确实选择了 - NotificationCenter not called in CollectionViewCell did select
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM