Created
August 21, 2012 15:19
-
-
Save keefo/3416457 to your computer and use it in GitHub Desktop.
Simplifying NSNotificationCenter block
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
@interface NSNotificationCenter (ObserverAdditions) | |
- (void)registerObserver:(id)observer | |
forName:(NSString *)name | |
object:(id)obj | |
queue:(NSOperationQueue *)queue | |
usingBlock:(void (^)(NSNotification *))block; | |
- (void)unregisterObserversForObserver:(id)observer; | |
- (void)unregisterObserver:(id)observer name:(NSString *)aName; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "NSNotificationCenter+ObserverAdditions.h" | |
#import <objc/runtime.h> | |
static char notificationObserverKey; | |
@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]; | |
} | |
- (void)unregisterObserversForObserver:(id)registeredObserver | |
{ | |
[self removeObserversForObserver:registeredObserver forName:nil]; | |
} | |
- (void)unregisterObserver:(id)obsrvr name:(NSString *)aName | |
{ | |
[self removeObserversForObserver:obsrvr forName:aName]; | |
} | |
#pragma mark- Internal Methods | |
- (NSMutableArray *)observationRecordListForObserver:(id)obsrv | |
{ | |
NSMutableArray *observers = objc_getAssociatedObject(obsrv, ¬ificationObserverKey); | |
if(!observers) | |
{ | |
observers = [NSMutableArray array]; | |
objc_setAssociatedObject(obsrv, ¬ificationObserverKey, observers, OBJC_ASSOCIATION_RETAIN); | |
} | |
return observers; | |
} | |
- (void)removeObserversForObserver:(id)observr forName:(NSString *)name | |
{ | |
NSMutableIndexSet *indexes = [NSMutableIndexSet indexSet]; | |
NSMutableArray *observersArr = [self observationRecordListForObserver:observr]; | |
for(int index = 0; index < [observersArr count]; index++) | |
{ | |
NSDictionary *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]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment