is removeObserver necessary on dealloc? - iphone

In one of my view controller, it adds itself as observer of UITextViewTextDidEndEditingNotification notification, like the following does
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(done:) name:UITextViewTextDidEndEditingNotification object:nil];
Now I am wondering - do I need to do the following when the view controller is dealloc'd
[[NSNotificationCenter defaultCenter] removeObserver:self];

yes, you should always remove any observers when they're being dealloc'd. otherwise the notification center will keep references to the now-dealloc'd objects around and continue to try to forward notifications to them.

Related

Posting NSNotification from object inside SKScene error

I have SKScene controlled by ViewController. Inside this SKScene I have an playing board object (derived from NSObject) with some procedures. I need to post a notification from one of it's procedures (playing board procedures). Inside ViewController I have an observer:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(rowBangEmitAtPosition:) name:#"rowBangN" object:nil];
and inside playing board's procedure I have :
[[NSNotificationCenter defaultCenter] postNotificationName:#"rowBangN" object:self userInfo:#{#"position":point}];
The object is part of SKScene (as variable). But this notification is never triggered. Can someone help me please.
I figured it out - when I put into posting code object:nil instead of object:self, it is working. i don't know why, but it works :).
Thanks to all.

NSNotificationCenter one post causes observers to be called twice

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

NSNotificationCenter in viewDidLoad: not working

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.

Determine if UIViewController is closing due to application exit?

Is there a way to test in viewWillDisappear if it's being called because the application is exiting, versus normal ways of it being dismissed? The method applicationWillTerminate in the App Delegate is called after the current view is closed. I want to do different things depending on whether it's being dismissed due to a IBAction or the user clicking the menu button.
Thanks!
You should use observe the UIApplicationWillTerminateNotification in your controller, set a flag, and then check for the flag in your viewWillDisappear implementation.
NSNotificationCenter* defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self
selector:#selector(applicationWillTerminate:)
name:UIApplicationWillTerminateNotification
object:nil];
I haven't used it for your purposes yet, but the UIApplicationWillResignActiveNotification notification might occur before applicationWillTerminate is called.
Just throw...
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(applicationWillResign:) name:UIApplicationWillResignActiveNotification object:NULL];
... into your UIViewController to test it out.