Last active
October 24, 2016 18:17
-
-
Save jchudzynski/8007142 to your computer and use it in GitHub Desktop.
KVO of NSOperationQueue
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
@interface KVOOperationQueue() | |
@property(nonatomic, strong) NSOperationQueue *queue; | |
@end | |
@implementation KVOOperationQueue | |
-(id)init{ | |
self = [super init]; | |
if(self){ | |
filePath = filename; | |
blockOperation = [[NSOperation alloc]init]; | |
//initialize the queue | |
_queue = [[NSOperationQueue alloc]init]; | |
//!!! adding observer of the operationCount property to self | |
[self.queue addObserver:self forKeyPath:@"operationCount" options:NSKeyValueObservingOptionNew context:nil]; | |
} | |
return self; | |
} | |
/* | |
This method observes changes to to all objects and keypaths, in our case we are looking for operationCount property | |
The change dictionary stores old and new values of the observed property. If the new value is equal to 0 it means that queue | |
doesn't have any more completed operations. | |
*/ | |
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{ | |
if([keypath isEqualToString:@"operationCount"]){ | |
if([[change objectForKey:@"new"] isEqual:@0]){ | |
NSLog(@"Completed"); | |
} | |
} | |
} | |
//This method adds operations to the operation queue | |
-(void)someOperationsWithQueues{ | |
for(int i =1; i<1000;i++){ | |
[_queue addOperationWithBlock:^{ | |
//background operation | |
}]; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment