forked from steipete/PSFoundation
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added methods from FoundationKit to safely remove an observer
- Loading branch information
Showing
3 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Part of FoundationKit http://foundationk.it | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
/** | ||
This category adds safe methods to NSObject for removing an observer. If you remove an observer, that was already | ||
removed or got never added you get an NSRangeException. These methods wrap the calls to removeObserver in a try-catch | ||
block and only log this exception | ||
*/ | ||
|
||
@interface NSObject (FKObserver) | ||
|
||
/** | ||
Safely calls removeObserver:forKeyPath: on NSObject | ||
@param observer The observer to remove | ||
@param keyPath The keyPath the observer observes | ||
*/ | ||
- (void)safeRemoveObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath; | ||
|
||
/** | ||
Safely calls removeObserver:forKeyPath:context: on NSObject | ||
@param observer The observer to remove | ||
@param keyPath The keyPath the observer observes | ||
@param context The context of the observation | ||
*/ | ||
- (void)safeRemoveObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath context:(void *)context; | ||
|
||
@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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#import "NSObject+FKObserver.h" | ||
|
||
@implementation NSObject (FKObserver) | ||
|
||
- (void)safeRemoveObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath { | ||
@try { | ||
[self removeObserver:observer forKeyPath:keyPath]; | ||
} | ||
@catch (NSException *exception) { | ||
DDLogWarn(@"Tried to remove Observer '%@' for keyPath '%@' and got Exception: %@", observer, keyPath, exception); | ||
} | ||
} | ||
|
||
- (void)safeRemoveObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath context:(void *)context { | ||
@try { | ||
if ([self respondsToSelector:@selector(removeObserver:forKeyPath:context:)]) { | ||
[self removeObserver:observer forKeyPath:keyPath context:context]; | ||
} else { | ||
[self removeObserver:observer forKeyPath:keyPath]; | ||
} | ||
} | ||
@catch (NSException *exception) { | ||
DDLogWarn(@"Tried to remove Observer '%@' for keyPath '%@' in context and got Exception: %@", observer, keyPath, exception); | ||
} | ||
} | ||
|
||
@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