Skip to content

Instantly share code, notes, and snippets.

@0x8badf00d
Created September 14, 2012 04:02
Show Gist options
  • Save 0x8badf00d/3719715 to your computer and use it in GitHub Desktop.
Save 0x8badf00d/3719715 to your computer and use it in GitHub Desktop.
NSNotificationCenter+ObserverAdditions
#import <Foundation/Foundation.h>
@interface NSNotificationCenter (ObserverAdditions)
- (void)registerObserver:(id)observer
forName:(NSString *)name
object:(id)obj
queue:(NSOperationQueue *)queue
usingBlock:(void (^)(NSNotification *note))block;
- (void)unregisterObserversForObserver:(id)observer;
- (void)unregisterObserversForName:(NSString *)aName;
- (void)unregisterObserver:(id)observer name:(NSString *)aName;
@end
#import "NSNotificationCenter+ObserverAdditions.h"
#import <objc/runtime.h>
static char notificationObserverKey;
static char notificationNameKey;
@implementation NSNotificationCenter (ObserverAdditions)
- (void)registerObserver:(id)registeredObserver
forName:(NSString *)name
object:(id)obj
queue:(NSOperationQueue *)queue
usingBlock:(void (^)(NSNotification *note))block
{
id observer = [self addObserverForName:name object:obj queue:queue usingBlock:block];
// Data Structure to track observer,notification name information
NSDictionary *observationRecord = [NSDictionary dictionaryWithObjectsAndKeys:observer,name,nil];
[[self observationRecordListForObserver:registeredObserver] addObject:observationRecord];
[[self observationRecordListForName:name] addObject:observationRecord];
}
- (void)unregisterObserversForObserver:(id)registeredObserver
{
[self removeObserversForObserver:registeredObserver forName:nil];
}
- (void)unregisterObserver:(id)obsrvr name:(NSString *)aName
{
[self removeObserversForObserver:obsrvr forName:aName];
}
- (void)unregisterObserversForName:(NSString *)aName
{
[self removeObserversForObserver:nil forName:aName];
}
#pragma mark- Internal Methods
// Hold array of Observers for Notification Observer
- (NSMutableArray *)observationRecordListForObserver:(id)obsrv
{
NSMutableArray *observers = objc_getAssociatedObject(obsrv, &notificationObserverKey);
if(!observers)
{
observers = [NSMutableArray array];
objc_setAssociatedObject(obsrv, &notificationObserverKey, observers, OBJC_ASSOCIATION_RETAIN);
}
return observers;
}
// Hold an array of Observers for Notification Name
- (NSMutableArray *)observationRecordListForName:(NSString *)name
{
NSMutableArray *observers = objc_getAssociatedObject(name, &notificationNameKey);
if(!observers)
{
observers = [NSMutableArray array];
objc_setAssociatedObject(name, &notificationNameKey, observers, OBJC_ASSOCIATION_RETAIN);
}
return observers;
}
- (void)removeObserversForObserver:(id)observr forName:(NSString *)name
{
if(observr)
{
NSMutableIndexSet *indexes = [NSMutableIndexSet indexSet];
NSMutableArray *observersArr = nil;
NSDictionary *observationRcrd = nil;
observersArr = [self observationRecordListForObserver:observr];
for(int index = 0; index < [observersArr count]; index++)
{
observationRcrd = [observersArr objectAtIndex:index];
if(name)
{
if([observationRcrd objectForKey:name])
{
[self removeObserver:[observationRcrd objectForKey:name]
name:name
object:nil];
[indexes addIndex:index];
}
}
else
{
for(id observer in [observationRcrd allValues])
{
[self removeObserver:observer];
[indexes addIndex:index];
}
}
}
[observersArr removeObjectsAtIndexes:indexes];
observersArr = nil;
observationRcrd = nil;
indexes = nil;
}
else
{
NSMutableIndexSet *indexes = [NSMutableIndexSet indexSet];
NSMutableArray *observersArr = nil;
NSDictionary *observationRcrd = nil;
observersArr = [self observationRecordListForObserverName:name];
for(int index = 0; index < [observersArr count]; index++)
{
observationRcrd = [observersArr objectAtIndex:index];
for(id observer in [observationRcrd allValues])
{
[self removeObserver:observer name:name object:nil];
[indexes addIndex:index];
}
}
[observersArr removeObjectsAtIndexes:indexes];
indexes = nil;
observersArr = nil;
observationRcrd = nil;
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment