Question

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];
Was it helpful?

Solution

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top