Skip to content
Browse files

Merge pull request #391 from ReactiveCocoa/less-lulzy-example

Less lulzy RAC-less parallel work example
  • Loading branch information...
2 parents dd7017c + a2444ef commit 239c3e1c9e29b5ee58cf2c8e61a346f1d7081b68 @jspahrsummers jspahrsummers committed Mar 25, 2013
Showing with 22 additions and 21 deletions.
  1. +22 −21 README.md
View
43 README.md
@@ -382,31 +382,31 @@ synchronization:
```objc
__block NSArray *databaseObjects;
__block NSArray *fileContents;
-
-dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
- @synchronized (self) {
- databaseObjects = [databaseClient fetchObjectsMatchingPredicate:predicate];
- if (fileContents != nil) {
- [self finishProcessingDatabaseObjects:databaseObjects fileContents:fileContents];
- NSLog(@"Done processing");
- }
- }
-});
-
-dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
+
+NSOperationQueue *backgroundQueue = [[NSOperationQueue alloc] init];
+NSBlockOperation *databaseOperation = [NSBlockOperation blockOperationWithBlock:^{
+ databaseObjects = [databaseClient fetchObjectsMatchingPredicate:predicate];
+}];
+
+NSBlockOperation *filesOperation = [NSBlockOperation blockOperationWithBlock:^{
NSMutableArray *filesInProgress = [NSMutableArray array];
for (NSString *path in files) {
[filesInProgress addObject:[NSData dataWithContentsOfFile:path]];
}
-
- @synchronized (self) {
- fileContents = [filesInProgress copy];
- if (databaseObjects != nil) {
- [self finishProcessingDatabaseObjects:databaseObjects fileContents:fileContents];
- NSLog(@"Done processing");
- }
- }
-});
+
+ fileContents = [filesInProgress copy];
+}];
+
+NSBlockOperation *finishOperation = [NSBlockOperation blockOperationWithBlock:^{
+ [self finishProcessingDatabaseObjects:databaseObjects fileContents:fileContents];
+ NSLog(@"Done processing");
+}];
+
+[finishOperation addDependency:databaseOperation];
+[finishOperation addDependency:filesOperation];
+[backgroundQueue addOperation:databaseOperation];
+[backgroundQueue addOperation:filesOperation];
+[backgroundQueue addOperation:finishOperation];
```
The above code can be cleaned up and optimized by simply composing signals:
@@ -429,6 +429,7 @@ RACSignal *fileSignal = [RACSignal start:^(BOOL *success, NSError **error) {
combineLatest:@[ databaseSignal, fileSignal ]
reduce:^(NSArray *databaseObjects, NSArray *fileContents) {
[self finishProcessingDatabaseObjects:databaseObjects fileContents:fileContents];
+ return nil;
}]
subscribeCompleted:^{
NSLog(@"Done processing");

0 comments on commit 239c3e1

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