Skip to content
Browse files

Added one-shot versions of the KVO addKVO… apis.

  • Loading branch information...
1 parent 7d49468 commit 0c9fe2871e7fc4571622219534d452e4a1a54c19 @schwa committed Dec 20, 2011
Showing with 52 additions and 0 deletions.
  1. +5 −0 NSObject_KVOBlock.h
  2. +24 −0 NSObject_KVOBlock.m
  3. +23 −0 Test/CTester.m
View
5 NSObject_KVOBlock.h
@@ -38,9 +38,14 @@ typedef void (^KVOBlock)(NSString *keyPath, id object, NSDictionary *change);
- (id)addKVOBlockForKeyPath:(NSString *)inKeyPath options:(NSKeyValueObservingOptions)inOptions handler:(KVOBlock)inHandler;
- (id)addKVOBlockForKeyPath:(NSString *)inKeyPath options:(NSKeyValueObservingOptions)inOptions identifier:(NSString *)inIdentifier handler:(KVOBlock)inHandler;
+/// One shot blocks remove themselves after they've been fired once.
+- (id)addOneShotKVOBlockForKeyPath:(NSString *)inKeyPath options:(NSKeyValueObservingOptions)inOptions handler:(KVOBlock)inHandler;
+- (id)addOneShotKVOBlockForKeyPath:(NSString *)inKeyPath options:(NSKeyValueObservingOptions)inOptions identifier:(NSString *)inIdentifier handler:(KVOBlock)inHandler;
+
- (void)removeKVOBlockForToken:(id)inToken;
- (void)removeKVOBlockForKeyPath:(NSString *)inKeyPath identifier:(NSString *)inIdentifier;
- (NSArray *)allKVOObservers;
@end
+
View
24 NSObject_KVOBlock.m
@@ -84,6 +84,30 @@ - (id)addKVOBlockForKeyPath:(NSString *)inKeyPath options:(NSKeyValueObservingOp
return(theKey);
}
+- (id)addOneShotKVOBlockForKeyPath:(NSString *)inKeyPath options:(NSKeyValueObservingOptions)inOptions handler:(KVOBlock)inHandler
+ {
+ __block id theToken = NULL;
+ KVOBlock theBlock = ^(NSString *keyPath, id object, NSDictionary *change) {
+ inHandler(keyPath, object, change);
+ [self removeKVOBlockForToken:theToken];
+ };
+
+ theToken = [self addKVOBlockForKeyPath:inKeyPath options:inOptions handler:theBlock];
+ return(theToken);
+ }
+
+- (id)addOneShotKVOBlockForKeyPath:(NSString *)inKeyPath options:(NSKeyValueObservingOptions)inOptions identifier:(NSString *)inIdentifier handler:(KVOBlock)inHandler
+ {
+ __block id theToken = NULL;
+ KVOBlock theBlock = ^(NSString *keyPath, id object, NSDictionary *change) {
+ inHandler(keyPath, object, change);
+ [self removeKVOBlockForToken:theToken];
+ };
+
+ theToken = [self addKVOBlockForKeyPath:inKeyPath options:inOptions identifier:inIdentifier handler:theBlock];
+ return(theToken);
+ }
+
- (void)removeKVOBlockForToken:(id)inToken
{
CKVOBlockHelper *theHelper = [CKVOBlockHelper helperForObject:self create:NO];
View
23 Test/CTester.m
@@ -36,6 +36,7 @@
@interface CTester ()
- (void)testIdentifiers;
- (void)testTokens;
+- (void)testOneShot;
@end
#pragma mark -
@@ -48,10 +49,13 @@ - (void)test
{
[self testIdentifiers];
[self testTokens];
+ [self testOneShot];
}
- (void)testIdentifiers
{
+ NSLog(@"##### IDENTIFIERS #####");
+
[self addKVOBlockForKeyPath:@"testValue" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld identifier:@"my_handler" handler:^(NSString *keyPath, id object, NSDictionary *change) {
NSLog(@"I see you changed value from \"%@\" to \"%@\"", [change objectForKey:NSKeyValueChangeOldKey], [change objectForKey:NSKeyValueChangeNewKey]);
}];
@@ -67,6 +71,8 @@ - (void)testIdentifiers
- (void)testTokens
{
+ NSLog(@"##### TOKENS #####");
+
id theToken = [self addKVOBlockForKeyPath:@"testValue" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld handler:^(NSString *keyPath, id object, NSDictionary *change) {
NSLog(@"I see you changed value from \"%@\" to \"%@\"", [change objectForKey:NSKeyValueChangeOldKey], [change objectForKey:NSKeyValueChangeNewKey]);
}];
@@ -80,4 +86,21 @@ - (void)testTokens
[self removeKVOBlockForToken:theToken];
}
+- (void)testOneShot
+ {
+ NSLog(@"##### ONE SHOT #####");
+
+ id theToken = [self addOneShotKVOBlockForKeyPath:@"testValue" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld handler:^(NSString *keyPath, id object, NSDictionary *change) {
+ NSLog(@"I see you changed value from \"%@\" to \"%@\"", [change objectForKey:NSKeyValueChangeOldKey], [change objectForKey:NSKeyValueChangeNewKey]);
+ }];
+
+ self.testValue = @"A horse";
+ self.testValue = @"is a horse";
+ self.testValue = @"of course";
+ self.testValue = @"of course.";
+ self.testValue = NULL;
+
+ [self removeKVOBlockForToken:theToken];
+ }
+
@end

0 comments on commit 0c9fe28

Please sign in to comment.
Something went wrong with that request. Please try again.