Is it possible to get the list of observers (objects and selectors) for a given notification name? (NSNotificationCenter)
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- back button text does not change
- iOS (objective-c) compression_decode_buffer() retu
- how to find the index position of the ARRAY Where
相关文章
- 现在使用swift开发ios应用好还是swift?
- TCC __TCCAccessRequest_block_invoke
- xcode 4 garbage collection removed?
- Unable to process app at this time due to a genera
- How can I add media attachments to my push notific
- didBeginContact:(SKPhysicsContact *)contact not in
- Custom Marker performance iOS, crash with result “
- Converting (u)int64_t to NSNumbers
I don't think there is an (official) way of retrieving the list of observers for a given notification name from
NSNotificationCenter
. However, you could create a subclass ofNSNotificationCenter
and then override the following methods:+ defaultCenter
- addObserver:selector:name:object
- addObserverForName:object:queue:usingBlock:
- removeObserver:
- removeObserver:name:object
In the overriding implementations of the instance methods, you would then keep track of the observers for a given notification name using a dictionary. In each overridden instance method you would finally call
NSNotificationCenter
's respectivesuper
method. Additionally, you would provide a method to retrieve your own list of observers for the given name, for example:However, there are two issues with this approach: first,
NSMutableDictionary
would retain all observers in a naive implementation, which is probably not the same behaviorNSNotificationCenter
implements. Second, you would have to change the code that gets the default notification center by[NSNotificationCenter defaultCenter]
(or any otherNSNotificationCenter
instance) so as to use your custom subclass.Note that the first issue is solvable using a
CFDictionary
with weak reference callbacks, a container class with a weak reference to the respective observer, or, if you are in a garbage collected environment on Mac OS X, anNSHashTable
.There is no public API to query
NSNotificationCenter
about the list of current observers for any object or notification.The previous answer outlines a solution and goes to some level of detail regarding the ownership of observers, in a subclass of
NSNotificationCenter
designed to collect and provide such information.However, this solution can only be used with your own code, that will call the subclass of
NSNotiicationCenter
. What about other code, both in the system and external libraries who use the baseNSNotificationCenter
for registering/unregistering for notifications?I suggest instead of subclassing
NSNotificationCenter
, using a bit of low-level ObjC to swizzle the method implementations of originalNSNotifictionCenter
, replacing them with our own implementations, that will work more-or-less as described in the previous answer, and will call the original implementations as their last act.Here's how to do this: http://nshipster.com/method-swizzling/
Then, you can be sure you get ALL the observers of some notification, and that your code is portable and usable with 3rd party code that directly uses
NSNotificationCenter
.(iOS 9, Swift 3) If you want to find out which observers are currently registered in
NotificationCenter
, break and print its debug description:Each line of the output will contain (Notification) Name, Object, Observer, Options. Multiple calls to
NotificationCenter.default.addObserver
with someNSNotification.Name
will result in multiple entries in this list.NB. while this might prove useful information when debugging, I would not advise to manage observers at runtime using this output.
(source: answer based on useyourloaf)
Instead of using NSNotificationCenter, you can try this ObserversCenter. And you can get the list of observers.
About ObserverCenter:
I created a category on NSNotificationCenter and swizzled the addObserver:::: method.
This is only for debugging and should never be in production code as it will lead to retain cycles
Have you tried the
observationInfo
property of NSObject?