ios 如何避免添加多个 NSNotification 观察者?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5658426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:34:32  来源:igfitidea点击:

How to avoid adding multiple NSNotification observer?

ioscocoansnotifications

提问by Boon

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?

现在,API 似乎没有提供一种方法来检测是否已经为特定的 NSNotification 添加了观察者。除了在您的一端维护一个标志以进行跟踪之外,避免添加多个 NSNotification 观察者的最佳方法是什么?有没有人已经创建了一个类别来促进这一点?

回答by futureelite7

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:

防止添加重复观察者的一种方法是在再次添加之前为目标/选择器显式调用 removeObserver。我想您可以将其添加为类别方法:

@interface NSNotificationCenter (UniqueNotif)

- (void)addUniqueObserver:(id)observer selector:(SEL)selector name:(NSString *)name object:(id)object {

        [[NSNotificationCenter defaultCenter] removeObserver:observer name:name object:object];
        [[NSNotificationCenter defaultCenter] addObserver:observer selector:selector name:name object:object];

}

@end

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.

这假设您只会为任何通知名称的每个目标添加一个唯一的观察者,因为它将删除该通知名称的任何现有观察者。

回答by dimpiax

Swift 3, 4:

斯威夫特 3、4:

import Foundation

extension NotificationCenter {
    func setObserver(_ observer: AnyObject, selector: Selector, name: NSNotification.Name, object: AnyObject?) {
        NotificationCenter.default.removeObserver(observer, name: name, object: object)
        NotificationCenter.default.addObserver(observer, selector: selector, name: name, object: object)
    }
}


Swift 2:

斯威夫特 2:

import Foundation

extension NSNotificationCenter {
    func setObserver(observer: AnyObject, selector: Selector, name: String?, object: AnyObject?) {
        NSNotificationCenter.defaultCenter().removeObserver(observer, name: name, object: object)
        NSNotificationCenter.defaultCenter().addObserver(observer, selector: selector, name: name, object: object)
    }
}

回答by user44776

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.

Upvoted 的答案extension NotificationCenter { ... }对我不起作用,因为我的应用程序在每次发布通知时都在创建一个新的 viewController 实例(它有一个通知观察者),因此在 viewController 的新实例上删除观察者显然不会工作。具有通知观察者的 viewController 的先前实例被调用。

The below worked for me, since this was removing the Notification Observer as soon as the view was being disappeared.

以下对我有用,因为这会在视图消失后立即删除通知观察器。

// Notification observer added 

override func viewWillAppear(_ animated: Bool) {

    NotificationCenter.default.addObserver(self, selector: #selector(self.someFunc(notification:)), name: Notification.Name("myNotification"), object: nil)


}


// Notification observer removed 

override func viewWillDisappear(_ animated: Bool) {

    NotificationCenter.default.removeObserver(self, name: Notification.Name("myNotification"), object: nil)


}