RNConcurrentBlockOperation is a simple NSOperation subclass, similar to NSBlockOperation. It allows a block of work that is potentially asynchrounous to be submited into a NSOperationQueue for parallel execution.
Sample usage:
NSOperationQueue *queue = [NSOperationQueue new];
queue.maxConcurrentOperationCount = 5;
// Regular usage, only finishes the operation
[queue addOperation:[RNConcurrentBlockOperation operationWithBlock:^(RNCompletionBlock completion) {
NSLog(@"Concurrent op started");
//Some async operation
//... ... ...
//Async operation completed
completion(nil);
}]];
// Cancelled operation, cancels then finish the operation
[queue addOperation:[RNConcurrentBlockOperation operationWithBlock:^(RNCompletionBlock completion) {
NSLog(@"Cancellable op started");
//Some async operation
//... ... ...
//Something happened (i.e user cancelled, network outage), and we want to bail
//Async operation cancelled
NSError *error = [NSError new]; //Some possible error
completion(@{RNOperationStatusKey: RNOperationStatusCanceled, RNOperationErrorKey: error});
}]];
// Store the operation value in the userInfo dictionary and finishes it.
[queue addOperation:[RNConcurrentBlockOperation operationWithBlock:^(RNCompletionBlock completion) {
NSLog(@"Concurrent op started with result");
NSString *result = @"A string generated by the async operation";
completion(@{RNOperationResultKey: result});
}]];