Created
March 23, 2013 15:20
-
-
Save joshaber/5228077 to your computer and use it in GitHub Desktop.
Parallel dependent tasks
This file contains hidden or 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
__block NSArray *databaseObjects; | |
__block NSArray *fileContents; | |
NSOperationQueue *backgroundQueue = [[NSOperationQueue alloc] init]; | |
NSBlockOperation *operation1 = [NSBlockOperation blockOperationWithBlock:^{ | |
databaseObjects = [databaseClient fetchObjectsMatchingPredicate:predicate]; | |
}]; | |
NSBlockOperation *operation2 = [NSBlockOperation blockOperationWithBlock:^{ | |
NSMutableArray *filesInProgress = [NSMutableArray array]; | |
for (NSString *path in files) { | |
[filesInProgress addObject:[NSData dataWithContentsOfFile:path]]; | |
} | |
fileContents = [filesInProgress copy]; | |
}]; | |
NSBlockOperation *finish = [NSBlockOperation blockOperationWithBlock:^{ | |
[self finishProcessingDatabaseObjects:databaseObjects fileContents:fileContents]; | |
NSLog(@"Done processing"); | |
}]; | |
[finish addDependency:operation1]; | |
[finish addDependency:operation2]; | |
[backgroundQueue addOperation:operation1]; | |
[backgroundQueue addOperation:operation2]; | |
[backgroundQueue addOperation:finish]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment