Skip to content

Instantly share code, notes, and snippets.

@nixta
Last active August 29, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nixta/10495528 to your computer and use it in GitHub Desktop.
Save nixta/10495528 to your computer and use it in GitHub Desktop.
NSNotificationsProvider: Subscribe to multiple notifications on an object with one call.

NSNotificationsProvider Category

By including the .h and .m files in this Gist, you can register a listener to numerous notifications on a notification emitter with a single call, providing a dictionary where the keys are the notification names, and the value are @selectors. Since you cannot directly put a @selector into a container, a macro is provided to turn the selector into an NSString.

Now you can do this:

[self.clusterLayer registerListener:self forNotifications:@{
    AGSClusterLayerDataLoadingProgressNotification: strSelector(dataLoadProgress:),
    AGSClusterLayerDataLoadingErrorNotification: strSelector(dataLoadError:),
    AGSClusterLayerClusteringProgressNotification: strSelector(clusteringProgress:)
}];

instead of this:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(dataLoadProgress:)
                                             name:AGSClusterLayerDataLoadingProgressNotification
                                           object:self.clusterLayer];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(dataLoadError:)
                                             name:AGSClusterLayerDataLoadingErrorNotification
                                           object:self.clusterLayer];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(clusteringProgress:)
                                             name:AGSClusterLayerClusteringProgressNotification
                                           object:self.clusterLayer];
#import <Foundation/Foundation.h>
#define strSelector(a) NSStringFromSelector(@selector(a))
@interface NSObject (NFNotificationsProvider)
-(void)registerListener:(id)listener forNotifications:(NSDictionary *)notificationSelectors;
-(void)unRegisterListener:(id)listener fromNotifications:(NSArray *)notificationNames;
@end
#import "NSObject+NFNotificationsProvider.h"
@implementation NSObject (NFNotificationsProvider)
-(void)registerListener:(id)listener forNotifications:(NSDictionary *)notificationSelectors {
for (id notificationName in notificationSelectors) {
[[NSNotificationCenter defaultCenter] addObserver:listener
selector:NSSelectorFromString(notificationSelectors[notificationName])
name:notificationName
object:self];
}
}
-(void)unRegisterListener:(id)listener fromNotifications:(NSArray *)notificationNames {
for (id notificationName in notificationNames) {
[[NSNotificationCenter defaultCenter] removeObserver:listener
name:notificationName
object:self];
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment