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.
Related
I have the following code:
[[NSNotificationCenter defaultCenter] postNotificationName:kNewsfeedFetchCompleted object:self userInfo:userinfo];
only this, no where else. And here's how I set the observer:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(newsfeedFetchCompleted:) name:kNewsfeedFetchCompleted object:nil];
question is when I do one post the newsfeedFetchCompleted is called twice.. how is this even possible?
This is possible when your code for addObserver is executed twice. The notification function will be called as many times as it is registered.
So make sure your code for adding observer is executed for once only. So, you can keep it in viewDidLoad or init method.
If you are putting it in viewWillAppear then remove observer in viewWillDisAppear.
before you add observer, make sure you remove the previous observer added.
[[NSNotificationCenter defaultCenter]removeObserver:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(newsfeedFetchCompleted:) name:kNewsfeedFetchCompleted object:nil];
It is possible if you have added the same observer multiple times for the newsfeedFetchCompleted notification. You should match your addObserver calls with removeObserver calls.
For example if you added the observer in viewWillAppear/viewWillDidAppear/ViewDidLoad of a UIViewController, you should remove it in viewWillDisappear/viewDidDisappear/ViewDidUnload.
The corresponding remove call for addObserver, is removeObserver:name:object:
More info can be found in the NSNotificationCenter docs
[[NSNotificationCenter defaultCenter] postNotificationName:kNotificationForbidAdvPost object:nil];
I define postNotificationName:kNotificationForbidAdvPost in a .h file
Im my subclass of NSApplication, I import this .h file. And have a observer of this Notification
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(changeToRecordingStatus) name:kNotificationForbidAdvPost object:nil];
I am sure that the observer was added first, and then post the notification.
And I see the [UINotification default] in Debug. It have same memory address in two part of code.
Is there some mistake I have?
Thanks!
The documentation is pretty clear when it states that
The method specified by
notificationSelector must have one and
only one argument (an instance of
NSNotification).
http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/Reference/Reference.html#//apple_ref/doc/uid/20000219-CHDCIGGB
So, I see your selector you are registering is changeToRecordingStatus when it must be changeToRecordingStatus:
- (void) changeToRecordingStatus: (NSNotification *) notification;
I have try that add a arugument, but it is still not worked.
I create a new project and use a no-argument function to response Notification, it is worked normally.
The reason why I cannot receive the Notification is that a removeObserver method was called at one part which I didn't notice.
Thanks for all your reply.
for some reason this code isn't working in viewDidLoad, but will work in viewWillAppear. Any ideas?
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(wakeUp:)
name:UIApplicationWillEnterForegroundNotification
object:nil];
Thank you
you're definitely sure viewDidLoad is being invoked?
For my case is that I put the removeObserver method inside didReceiveMemoryWarning method, and if I take a picture or do something else, this method fires out of my expect. So, now I always remove notification observer at dealloc stage.
I set up a notification observer in my view controller's init method like so:
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(saveState)
name:UIApplicationWillResignActiveNotification
object:nil];
Where is the best place to call removeObserver:name:object: for this notification. I'm currently calling it in my dealloc method, but wanted to know if that might cause problems.
No, you got it right. dealloc is the correct location to remove notification observers (unless you have some specific reason to need to remove the observer earlier).
You can always remove the observer in viewWillDisappear:, or when you are done using it and have no other need for it, you can place it in a function.
If the -saveState only need to execute once when active, then you can removeObserver inside the -saveState.
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 :)