How to use NSNotification in following scenario (IPhone SDK)? - iphone

Considering real life situation, suppose i have assigned some work to 3 people(say Person A, Person B, Person C), instead of waiting for them to complete a task each, i want that when each Person completes all assigned tasks, he/she will notify me distinctly. So that i can take further decision based on his/her task.
I want to implement this situation in code, with out using separate thread and delegates, i mean using NSNotification.
How can i do this stuff with programming, can u solve above situation using code (iPhone SDK-Objective C)?

[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(myMovieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer.moviePlayer];
- (void)myMovieFinishedCallback:(NSNotification*)aNotification
{
// Release the movie instance created in playMovieAtURL
MPMoviePlayerController *theMovie = [aNotification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:theMovie];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIDeviceOrientationDidChangeNotification
object:theMovie];
[theMovie stop];
[theMovie.view removeFromSuperview];
}
Like this you can use the NSNotification. Hope you understand.

As long as you are not using separate threads (or some kind of simulated asynchronicity) the 3 persons will need to wait on each other and using notifications doesn't really make sense.

Related

Which is a better way to remove Notification observer

I usually use NSNotification like the sample below:
In viewDidLoad:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(foo:) name:kName1 object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(bar:) name:kName2 object:nil];
In viewDidUnload and dealloc:
[[NSNotificationCenter defaultCenter] removeObserver:self];
But a friend told me that I should not use [[NSNotificationCenter defaultCenter] removeObserver:self]; because it will remove all the observers including the super class's . He suggested me to use the following code to remove observer one by one.
[[NSNotificationCenter defaultCenter] removeObserver:self name:kName1 object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:kName2 object:nil];
I've checked the ASIHttpRequest library's code ( https://github.com/pokeb/asi-http-request ). It follows my friends' suggestion.
I want to know if my friend is right or not? In my opinion, since the current instance will be unload or dealloc, the super class's notification is also useless. And is there any system UIViewController subclass use notification?
Your friend is 100% correct. Though, it does not matter if you remove all notification observations in dealloc.
You mentioned viewDidUnload, and there the case is completely different, because the unloaded object will stay alive, and you don't know when the notification observations of the superclass are added again. If they are added in viewDidLoad you won't have a problem. If they are added in an init method you just lost a bunch of important notification observations.
Removing observations with specific names is good practice and should be done from the beginning.
When you want to remove all notification you use,
[[NSNotificationCenter defaultCenter] removeObserver:self];
If you want to remove a particular notification you use,
[[NSNotificationCenter defaultCenter] removeObserver:self name:kName1 object:nil];
When you no longer in need of any notification the first approach is simple.
As the object is going away, it's safe to use [[NSNotificationCenter defaultCenter] removeObserver:self]; in the dealloc method.
In ViewDidUnload method, you'd better remove each observer one by one as a reference to the controller is still around (and your corresponding viewDidLoad should add them all back in).
I use the first way, I never thought about whether it was right or not. If dealloc is being called, then the object (super as well) is going to be deallocated anyway. What you definitely DON'T want is the NSNotification being sent to a deallocated instance.

Do I need to remove an observer from the NSNotificationCenter once, or as many times as it was added?

in my viewDidLoad, I add my controller as an observer for two notifications:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(checkNetworkStatus:) name:NetworkStatusChangedNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(checkLocationStatus:) name:LocationStatusChangedNotification object:nil];
in my dealloc, should I remove it once, or twice? The removeObserver method doesn't seem to specify a particular notification.
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter] removeObserver:self]; // is this required?
You need to remove it only once.
If you need it, you can also use -removeObserver:name:object: to stop observing just one of the notifications.
Documentation is the best way to clear your doubts:
The following example illustrates how to unregister someObserver for all
notifications for which it had previously registered:
[[NSNotificationCenter defaultCenter] removeObserver:someObserver];
From Reference:
RemoveObserver:
Removes all the entries specifying a given observer from the receiver’s dispatch table.
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/Reference/Reference.html
so you need to call it only once

NSNotification in iphone

i am sending NSSNotifcation to another view controller in iPhone app but its observer method getting notified two times how its possible can any one guide me
i have use this code to post notification
[[NSNotificationCenter defaultCenter] postNotificationName:#"updateStatusOnFacebook" object:nil userInfo:nil];
and added observer
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(postToWall) name:#"updateStatusOnFacebook" object:nil];
Have you added the observer twice?
Which method are you calling addObserver:selector:object: in? If it's in viewWillAppear then this might be called more than once.
Your method will be called the same number of times that you have added an observer.
Try this:
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"updateStatusOnFacebook" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(postToWall) name:#"updateStatusOnFacebook" object:nil];
The other reason is that you might just be sending the notification twice :)
I had the same problem crop up, and read this question, but could only find the one call to add the observer anywhere in the project.
In our case, the observer was being added twice because the method the line was in was being called twice.
Make sure you step through your code, breaking on your addObserver:selector:name:object call, to ensure that you don't have an unexpected extra execution path to that call.

Single Notification to multiple objects?

Is there a way to post single notification and have two different objects execute two different methods?
[[NSNotificationCenter defaultCenter] postNotificationName:#"locationAdded" object:nil];
OBJECT ONE:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(reloadAnnotations:)
name:#"locationAdded"
object:nil];
OBJECT TWO:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(reloadAnnotations:)
name:#"locationAdded"
object:nil];
.
.
.
Or as I suspect would I need to post two separate notifications with unique names (i.e.)
[[NSNotificationCenter defaultCenter] postNotificationName:#"updateTable" object:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:#"updateMapView" object:nil];
The notification gets delivered to each observer, which has been added before posting it.
So you need only a single notification. The selectors can be different.
No, that's fine, a single notification is delivered to as many observers as there are registered for it.

Unknown iphone code

I am looking at a project which was provided me by my organization, for study.
The problem is that in this project I found some code which I never saw before.
Please tell me why the following code is written.
-(void)notifications
{
[[NSNotificationCenter defaultCenter] addObserver: self selector:
#selector(hideViews) name: #"Hide" object:nil];
}
This problem arose because this project has only some code for designing.
Sorry if this is a silly question...
You should read up on how notifications work in Cocoa. Consult Apple's documentation for more information: http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/Reference/Reference.html
Basically, NSNotificationCenter is a class that broadcasts NSNotifications from one object to potentially many observing objects. One object can post a notification
[[NSNotificationCenter defaultCenter] postNotificationName:#"NotificationName" object:self];
and other objects can listen for this notification.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(notificationHandler:) object:theObjectThatPostedTheNotification];
Then, when the first object posts the notification, NSNotificationCenter will notify the other observing object, and notificationHandler: gets called.