Is there a way to know if an object is already registered as an observer for a particular notification? In my implementation I have to add and remove the observers on the fly. For some reason, there is a random issue where the listener is receiving twice the same notification. I know I have to review my coding but It will be easier to fix for me if I could know this info. Thanks.
2 Answers
No. There is no way to query this information. If you need it, you need to keep track of that yourself.

- 182,031
- 33
- 381
- 347
-
Thanks for your answer. I did a test by registering two observers with the same handler in the same object and It is being called twice. I thought the notification center would resolve this kind of situations. I will try to track this but i still think that it would be nice if I could query that to the center. Thanks again. – Jorge Dec 07 '10 at 05:23
-
There is a solution for Jorge. As pointed out by SteveB. See another posting: http://stackoverflow.com/questions/4668372/coalescing-while-using-nsnotificationqueue – Wayne Lo Jun 07 '11 at 18:53
-
@Wayne That's a completely different issue. Jorge's question was about registering for the same notification twice. `NSNotificationQueue` is about coalescing multiple sent notifications into a single one. The two issues are not related. – Lily Ballard Jun 07 '11 at 20:34
-
Kevin, you are right. I got confused with the two. I apologize. – Wayne Lo Jun 09 '11 at 22:58
You might want to look into NSNotificationQueue. Here's the overview from Apple. It sounds like this could help you stop receiving duplicate notifications:
NSNotificationQueue objects (or simply notification queues) act as buffers for notification centers (instances of NSNotificationCenter). Whereas a notification center distributes notifications when posted, notifications placed into the queue can be delayed until the end of the current pass through the run loop or until the run loop is idle. Duplicate notifications can also be coalesced so that only one notification is sent although multiple notifications are posted. A notification queue maintains notifications (instances of NSNotification) generally in a first in first out (FIFO) order. When a notification rises to the front of the queue, the queue posts it to the notification center, which in turn dispatches the notification to all objects registered as observers.
Every thread has a default notification queue, which is associated with the default notification center for the task. You can create your own notification queues and have multiple queues per center and thread.

- 652
- 3
- 12
-
Steve, thank you for shading the light. You helped me to find this posting: http://stackoverflow.com/questions/4668372/coalescing-while-using-nsnotificationqueue – Wayne Lo Jun 07 '11 at 18:54
-