Permalink
Browse files

Compilation setting that allow to add multiple times the same observe…

…r - parameters pairs
  • Loading branch information...
1 parent 38e2498 commit 014f4d5fba63dbf1d1def17f74d4b67a6481405a Krzysztof Zablocki committed Apr 5, 2012
View
3 README.md
@@ -1,7 +1,7 @@
Purpose
--------------
SFObservers is an category extension that adds auto removal for Observer pattern in NSNotificationCenter and KVO. By including this into your project, you no longer need to manually remove observers when observer object is deallocated.
-It also prevents adding more than once the same parameters for observer, you won't add the same observer for the same notification and the same object ever again.
+By default it also prevents adding more than once the same observer - parameters pair, it can be disabled by setting SF_OBSERVERS_ALLOW_MULTIPLE_REGISTRATIONS to 1 in SFObservers.h
Supported OS & SDK Versions
-----------------------------
@@ -18,6 +18,7 @@ Installation
To use the SFObserver in your app, just drag the class files (demo files and assets are not needed) into your project. And include SFObservers.h in your project Prefix.pch file.
There is no need to call custom methods, you can include it into existing project and it will work fine.
+If you want to allow adding the same observer - parameters pairs, set SF_OBSERVERS_ALLOW_MULTIPLE_REGISTRATIONS to 1 in SFObservers.h
Tests
--------------
View
6 SFObservers/NSNotificationCenter+SFObservers.m
@@ -87,6 +87,7 @@ - (void)sf_addObserver:(id)observer selector:(SEL)aSelector name:(NSString *)aNa
}
__block __SFObserversNotificationObserverInfo *observerInfo = nil;
+#if !SF_OBSERVERS_ALLOW_MULTIPLE_REGISTRATIONS
//! don't allow to add many times the same observer
[observerInfos enumerateObjectsUsingBlock:^void(id obj, NSUInteger idx, BOOL *stop) {
__SFObserversNotificationObserverInfo *info = obj;
@@ -105,6 +106,11 @@ - (void)sf_addObserver:(id)observer selector:(SEL)aSelector name:(NSString *)aNa
NSAssert(NO, @"You shouldn't register twice for same notification, selector, name, object");
return;
}
+#else
+ observerInfo = [[__SFObserversNotificationObserverInfo alloc] init];
+ [observerInfos addObject:observerInfo];
+ AH_RELEASE(observerInfo);
+#endif
observerInfo.name = aName;
observerInfo.object = anObject;
View
6 SFObservers/NSObject+SFObservers.m
@@ -86,6 +86,7 @@ - (void)sf_addObserver:(id)observer forKeyPath:(NSString *)keyPath options:(NSKe
}
__block __SFObserversKVOObserverInfo *observerInfo = nil;
+#if !SF_OBSERVERS_ALLOW_MULTIPLE_REGISTRATIONS
//! don't allow to add many times the same observer
[observerInfos enumerateObjectsUsingBlock:^void(id obj, NSUInteger idx, BOOL *stop) {
__SFObserversKVOObserverInfo *info = obj;
@@ -104,6 +105,11 @@ - (void)sf_addObserver:(id)observer forKeyPath:(NSString *)keyPath options:(NSKe
NSAssert(NO, @"You shouldn't register twice for same keyPath, context");
return;
}
+#else
+ observerInfo = [[__SFObserversKVOObserverInfo alloc] init];
+ [observerInfos addObject:observerInfo];
+ AH_RELEASE(observerInfo);
+#endif
observerInfo.keyPath = keyPath;
observerInfo.context = aContext;
View
1 SFObservers/SFObservers.h
@@ -78,6 +78,7 @@
#endif
#define SF_OBSERVERS_LOG_ORIGINAL_METHODS 0
+#define SF_OBSERVERS_ALLOW_MULTIPLE_REGISTRATIONS 0
#import "NSObject+SFExecuteOnDealloc.h"
#import "NSNotificationCenter+SFObservers.h"

0 comments on commit 014f4d5

Please sign in to comment.