How to avoid adding multiple NSNotification observer?

前端 未结 3 1544
日久生厌
日久生厌 2020-11-30 04:19

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 multi

相关标签:
3条回答
  • 2020-11-30 05:01

    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:

    @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.

    0 讨论(0)
  • 2020-11-30 05:20

    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.

    // 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)
    
    
    }
    
    0 讨论(0)
  • 2020-11-30 05:23

    Swift 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:

    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)
        }
    }
    
    0 讨论(0)
提交回复
热议问题