Jorge
Jorge

Reputation: 2066

NSNotificationCenter question

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.

Upvotes: 3

Views: 1108

Answers (2)

SteveB
SteveB

Reputation: 662

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.

Upvotes: 2

Lily Ballard
Lily Ballard

Reputation: 185661

No. There is no way to query this information. If you need it, you need to keep track of that yourself.

Upvotes: 2

Related Questions