如何避免添加多个 NSNotification 观察者?
目前,API 似乎没有提供一种方法来检测是否已为特定 NSNotification 添加了观察者。除了在端维护一个标志来跟踪之外,避免添加多个 NSNotification 观察者的最佳方法是什么?有人已经创建了一个类别来促进这一点吗?
Right now the API doesn't seem to provide a way to detect if an observer has already been added for a particular NSNotification. What's the best way to avoid adding multiple NSNotification observers other than maintaining a flag on your end to keep track? Has anyone already created a category to facilitate this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
防止添加重复观察者的一种方法是在再次添加目标/选择器之前显式调用removeObserver。我想您可以将其添加为类别方法:
这假设您只会为任何通知名称的每个目标添加一个唯一的观察者,因为它将删除该通知名称的任何现有观察者。
One way to prevent duplicate observers from being added is to explicitly call removeObserver for the target / selector before adding it again. I imagine you can add this as a category method:
This assumes that the you will only add one unique observer to each target for any notification name, as it will remove any existing observers for that notification name.
Swift 5:
Swift 3-4:
Swift 2:
Swift 5:
Swift 3-4:
Swift 2:
带有
extension NotificationCenter { ... }
的 Upvoted 答案对我不起作用,因为每次发布通知时我的应用程序都会创建一个 viewController 的新实例(这有一个通知观察者),所以删除 viewController 的新实例上的观察者显然不起作用。先前具有通知观察者的 viewController 实例正在被调用。
下面的内容对我有用,因为一旦视图消失,就会删除通知观察者。
The Upvoted answer with
extension NotificationCenter { ... }
did not work for me, since my app was creating a new instance of a viewController (this had a Notification observer) every time a notification was posted, so removing an observer on a new instance of a viewController obviously doesn't work.Previous instances of the viewController that had Notification Observers were getting called.
The below worked for me, since this was removing the Notification Observer as soon as the view was being disappeared.
好吧,应该检查一下苹果文档。
https://developer.apple.com/documentation/foundation/notificationcenter/1411723- addobserver
因此,要确保通知已存在则不会添加
Well should have checked apple documentation.
https://developer.apple.com/documentation/foundation/notificationcenter/1411723-addobserver
So to make sure that the notification is not add if it already existing
Swift 5:
基于 @futureelite7 和 @dimpiax 之前的答案对 Swift 5 进行了一些调整
Swift 5:
A couple of tweaks for Swift 5 based on previous answers from @futureelite7 and @dimpiax