Warning for iOS/iPhone users about duplicate NSNotification observations

江枫思渺然 提交于 2019-12-13 12:23:42

问题


This isn't a question so much as a warning to others to save them some time.

NSNotificationCenter on iOS 3/iPhone OS 3 (I'm assuming also Mac OS X and iOS 4) has the following behavior:

If you register yourself multiple times for the exact specific notification, NSNotificationCenter will NOT recognize the redundancy and instead will fire off as many notifications to you as you've registered an observation for.

This is almost never the behavior you want to see and is almost always accidental.

Example:

I want my view controller to receive notifications from a singleton network object when new data comes in:

- (void) viewDidLoad 
{
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self
            selector:@selector(newDataArrived:) 
                name:NewDataArrivedNotification
              object:[NetworkListener sharedNetworkListener]];
}

but earlier I'd already put the same thing in viewWillAppear:

- (void) viewWillAppear
{
    [super viewWillAppear];

    [[NSNotificationCenter defaultCenter] addObserver:self
            selector:@selector(newDataArrived:)
                name:NewDataArrivedNotification
              object:[NetworkListener sharedNetworkListener]];
}

Note that it's exactly the same notification, resolving to the same observer, sender and notification name.

In this case, if I don't remove one of those addObserver calls, I'll get duplicate notifications to my view controller.

In a multi-threaded environment, this is a world of hurt. Trust me.

Just putting this out there in case there are others who run into something like this.


回答1:


NSNotificationCenter on iOS 3/iPhone OS 3 (I'm assuming also Mac OS X and iOS 4) has the following behavior:

If you register yourself multiple times for the exact specific notification, NSNotificationCenter will NOT recognize the redundancy and instead will fire off as many notifications to you as you've registered an observation for.

This is almost never the behavior you want to see and is almost always accidental.

Example:

I want my view controller to receive notifications from a singleton network object when new data comes in:

- (void) viewDidLoad 
{
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self
            selector:@selector(newDataArrived:) 
                name:NewDataArrivedNotification
              object:[NetworkListener sharedNetworkListener]];
}

but earlier I'd already put the same thing in viewWillAppear:

- (void) viewWillAppear
{
    [super viewWillAppear];

    [[NSNotificationCenter defaultCenter] addObserver:self
            selector:@selector(newDataArrived:)
                name:NewDataArrivedNotification
              object:[NetworkListener sharedNetworkListener]];
}

Note that it's exactly the same notification, resolving to the same observer, sender and notification name.

In this case, if I don't remove one of those addObserver calls, I'll get duplicate notifications to my view controller.

In a multi-threaded environment, this is a world of hurt. Trust me.

Just putting this out there in case there are others who run into something like this.




回答2:


You should and always clean up your observers.
The easiest way to do it is : [[NSNotificationCenter defaultCenter] removeObserver:self]
viewDidLoad is not a good place to add observers, because this functions may get called multiple times, this happens when viewDidUnload is triggered. A good place to put your addObservers in viewWillAppear, and removeObservers in viewWillDisappear.




回答3:


As you said yourself, NSNotificationCenter makes no check for duplicates, which may be annoying for some, but makes sense when concidering the full system behind it.

The same logic applies to adding targets to certain objects, but there is often a key recognition on those.

Thank you for the insight, and for a good, SEO-friendly warning :)



来源:https://stackoverflow.com/questions/3072811/warning-for-ios-iphone-users-about-duplicate-nsnotification-observations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!