测试已测试 | ✗ |
语言语言 | Obj-CObjective C |
许可证 | MIT |
发布最后发布 | 2014 年 12 月 |
由 Rafael Nobre 维护。
RNConcurrentBlockOperation 是一个简单的 NSOperation 子类,类似于 NSBlockOperation。它允许将潜在的异步工作块提交到 NSOperationQueue 以进行并行执行。
示例使用
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 = nil; //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});
}]];