Warning for iOS/iPhone users about duplicate NSNotification observations - iphone

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.

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.

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.

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

Related

NSNotificationCenter with respect to ViewWillAppear and ViewWillDisapper

I have a simple viewController that I want to listen for UIKeyboardWillHideNotification. Therefore I have the following code:
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillBeHidden)
name:UIKeyboardWillHideNotification object:nil];
}
- (void) keyboardWillBeHidden
{
[self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
}
I'm trying to decide when to remove the viewController as an notification center observer. I only need to know about the UIKeyboardWillHideNotification when the viewcontroller is on screen, thus I'm thinking about adding the following:
- (void) viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
Is this sufficient? Is there ever a chance that viewDidUnload or dealloc will get called while the viewController is still on screen? Note that I'm using a very basic UINavigationController for the flow of my app.
Registering the notification in viewWillAppear and unregistering it in viewWillDisappear seems to be a clean and symmetric solution to me.
Note that viewWillAppear can be called multiple times before dealloc (e.g. if another view controller is pushed onto your VC, or if you switch between tab bar controllers.) If you register the notification in viewWillAppear and unregister it only in dealloc then you will get duplicate registrations (compare Warning for iOS/iPhone users about duplicate NSNotification observations) and the registered selector is called multiple times for a single notification event.
I actually prefer the block-based observer registration method
addObserverForName:object:queue:usingBlock:
which returns an opaque object which is used for removing the observer again. Storing this return value into an instance variable of your view controller helps to keep track if the observer is already registered or not, and therefore helps to avoid duplicate registrations.
To answer your direct question, dealloc will never be called while your view is still on screen unless you directly call it which you shouldn't be.
dealloc will only be called when there are no strong pointers remaining that point to your viewController.
As Anoop Vaidya suggests, it is totally doable to put removeObserver in dealloc and be confident that dealloc won't get called while your viewController is on screen, and if it does... well you have much bigger problems than removing an observer
Edit: Since I can't actually reply to comments yet, when your viewController is off screen it is actually deallocated. It is then re-instantiated when it is called back on screen.
Edit: I'm wrong

What's the better way to addObserver/removeObserver with NSNotificationCenter?

I used to addObserver in viewDidLoad: and removeObserver in dealloc:. Code:
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(refreshData)
name:AnyNotification
object:nil];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:AnyNotification
object:nil];
}
But according to some articles said, it's better to addObserver in viewDidAppear: and removeObserver in viewDidDisappear:. Code:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(refreshData)
name:AnyNotification
object:nil];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:AnyNotification
object:nil];
}
So, what's the better way to addObserver/removeObserver?
this depends on your scenario, usually the best approach is to add in viewDidLoad and remove in dealloc and in viewDidUnload (deprecated in iOS 9.0, use dealloc only), but there are some cases when you have same method in different classes like UI effects and want to call only current screen's method using notification, then you will have to add the observer in viewWillAppear and remove it in viewWillDisappear or viewDidAppear/viewDidDisappear
Edit:
A note from comments, thanks #honey.
Though now since iOS 9, you no longer need to care about removing the observer. See Apple release notes: "In OS X 10.11 and iOS 9.0 NSNotificationCenter and NSDistributedNotificationCenter will no longer send notifications to registered observers that may be deallocated..
I would normally put it in -viewDidAppear: and -viewDidDisapear: (or -viewWillAppear: and -viewWillDisappear:) simply because in every case I came across I'm only interested in the notification if the view is actually displayed.
It's probably a premature optimisation (your code for handling the notification could take some time, but might be useless if the view is not displayed), but then it's also no more code - it's the same code just in a different method...
Don't forget NSKeyValueObservingOptionInitial. I use it with viewWillAppear/viewWillDisappear so my UI is always up-do-date, even if I hide that view controller, saving resources because I will not update it until is shown again.
The best approach using NSNotifications is adding the observer when you need to observe for notifications and remove them when you don't need them anymore.
This could be on viewDidLoad:, viewWillAppear:, or when the user taps some button etc.
I will give you a little example:
My app has a tabbar, and in some of the view controllers, I'm displaying some info downloaded from internet (a tweet for example). I also have a class pooling for new data from server each 2 minutes, and as the server had new data, I updated the info on database. I will not use a delegate pattern to listen to DB changes, because I have so many view controllers displaying data, and it will be a very bad design making the delegate an array and looping to pass the data for every view controller. So, in this specific scenario, the best to do is to post a notification telling every VC that new data has come.
If your VC removes the delegate when the view disappears, only the current one will receive the notification and update the displaying contents.
You obviously could update the contents of the other VCs before display, on viewWillAppear: for example, but doing this the VC contents will be updated not only when necessary, but each time you change tabs.
It was only one example, where I tried to show you that for NSNotifications, is difficult to advise you when to add or remove observers when we don't have the entire description of how you app behaviours.
-viewWillAppear: + -viewWillDisappear: is better than -viewDidAppear: + -viewDidDisapear:, because they are always called the same number of times.

nsnotificationcenter method fired more than once

i have a viewcontroller .In it there is a nsnotification observer in it. i am posting the notification from another viewcontroller.but the nsnotification observers selector get fired two or sometimes three times. My question is that when i use [view removeFromSuperview];
to remove this viewcontrollers view ,is the notification observer removed? I have given this method at the dealloc method of the viewcontroller class
- (void)dealloc {
[super dealloc];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
No.
that method will be called when the viewcontrollers retain count becomes 0
You should add another method that will be called when the view is removed from the other viewcontroller and call
[[NSNotificationCenter defaultCenter] removeObserver:self];
For the issue that the selector is called multiple times, I would need to see more code - make sure that the line of code thats posting the notification isnt being called multiple times
NSNotification registered to whole app (or even for all operating system), not to single view or viewcontroller. You have need for remove observer in your action if it won't longer used. In this case you can handle only one posted notification.

Standard idiom for property change notifications on iPhone

Lets say I have a simple object that represents the state of a puzzle. The puzzle can be either solved or unsolved. I have two controllers with references to this puzzle object and they are visually representing the state in two different ways - say an on/off switch and a red/green light.
When the 'solved' property of the puzzle changes, the controllers need to update their view to represent the current state. Is there a standard idiom for communication the state change from the puzzle object to the controller?
My initial intent was to declare a custom protocol and track observers in the puzzle object. When the solved property changed, iterate over all the observers and invoke a specific method on the protocol. This seems like a common enough pattern that there might be some built in support but I wasn't able to find exactly what I was looking for in the docs.
While both answers so far have concentrated on the use of NSNotification, and that's totally valid, there's another way to do what you want that's built-in to Cocoa objects: Key-Value Observing, or KVO. It's slightly lighter-weight and a little less "action at a distance". I prefer using it whenever possible for observing changes in my data model classes. YMMV.
If I'm understanding correctly you can use NSNotification to do what you want. In your puzzle class you can use postNotificationName to tell any class that's observing when the puzzle changes state. To register a class as an observer to the puzzle, use the addObserver and removeObserver methods. Here are the definitions for the three methods:
-(void) postNotificationName:(NSString *) aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo;
-(void) addObserver:(id)observer selector:(SEL)aSelector name:(NSString *)aName object:(id)anObject;
-(void) removeObserver:(id)observer name:(NSString *)aName object:(id)anObject;
Here is some example code to use these for your program:
In your puzzle class, in the function that changes state:
[[NSNotificationCenter defaultCenter] postNotificationName:#"puzzleChangedState" object:self userInfo:NULL]
// if you want to send out moreInfo, like other variables, use userInfo with a dictionary object
In your controllers, or views, etc... wherever you want to get the puzzle changed state message:
//In your constructor or initialization method, register this class with the puzzle class
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(handlePuzzleChangedState:) name:#"puzzleChangedState" object:nil];
This will add your controller to the NotificationCenter and when the puzzle class posts a notification of "puzzleChangedState", your controller's handlePuzzleChangedState: method will be invoked.
Here is the handlePuzzleChangedState: function:
-(void) handlePuzzleChangedState:(NSNotification *) notification
{
//handle your puzzle state change here
}
If you want more help here's the docs:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Notifications/Introduction/introNotifications.html#//apple_ref/doc/uid/10000043i
Hope it works out!
Instead of a custom protocol, you can use notifications as follows.
In your controller, in viewDidLoad register yourself as an observer
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(puzzleStateDidChange:)
name:#"PuzzleStateDidChange" object:nil];
then implement
- (void)puzzleStateDidChange:(NSNotification *)notification
{
// set your switch and light according to the state notified
puzzleState = notification.object;
...
}
and finally add
[[NSNotificationCenter defaultCenter] removeObserver:self];
to the dealloc method to unregister yourself as an observer.
Now you are ready to receive notifications and react accordingly. What is missing is the code to be added to your puzzle object. Each time the state changes in the puzzle object,
use something like
[[NSNotificationCenter defaultCenter] postNotificationName:#"PuzzleStateDidChange" object:yourPuzzleState];
to notify your controllers about the state change.

addobserver / removeobserver query

Is it OK to use -removeObserver: first and then call -addObserver: with the same name? Or is it a rule to have -addObserver: first before -removeObserver:?
I tried it using OS 4.0 and it seems OK (no crash, warnings... etc.).
-(void) setObserver
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:OBSERVER_NAME object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector: #selector(selectorName)
name:OBSERVER_NAME
object:nil];
}
The reason is to prevent two observers with the same selectorName method being called twice assuming that -setObserver method was called again if its inside -viewDidLoad and a memory warning was issued.
Also, do I need to invoke -removeObserver: during -dealloc?
If you're getting -selectorName invoked twice, there's probably some other issue with your code. And even if Cocoa will be graceful about removing observers you haven't added yet, I wouldn't actually do that.
As user tonklon says in a comment, you should probably remove the observer in -viewDidUnload. As to your last question, it's also reasonable to remove the observer in -dealloc, though it's good practice to structure your code such that things like removing observers happen at a deterministic time rather than as a side-effect of memory management.