[英]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.