Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the Observer in NSNotification called twice....?

If the class is a custom class, after posting the notification, the selector corresponding to the observer is called twice.. Is there any better solution so that the selector is called only once?

like image 421
Ganesh Nayak Avatar asked Aug 25 '09 15:08

Ganesh Nayak


3 Answers

If the selector is called twice, you've probably registered for it twice.

like image 90
Jonathon Avatar answered Oct 15 '22 16:10

Jonathon


If the observer class is registered for a notification by name, but not against a specific object, it will receive multiple messages, as it will be invoked every time the notification occurs, regardless of the originating object.

Likewise, if the observer is registered against a specific object, but not against a named notification, it will be messaged every time there is a notification concerning that object.

An alternative mechanism for being informed of changes in a distant state, is Key Value observing -http://developer.apple.com/documentation/Cocoa/Conceptual/KeyValueObserving/KeyValueObserving.html

like image 25
cms Avatar answered Oct 15 '22 16:10

cms


What worked for me was a couple of things:

1-In the class where the addObserver registration call is made, was to add a specific object.

2-In the class where the postNotification call is made, as the same type of object.

I also do my postNotificaiton in a dispatchAsync call as well, but that's not related to your problem.

like image 1
Fadi Hatem Avatar answered Oct 15 '22 16:10

Fadi Hatem