假设有一个带有完成块的方法可以执行:
[container insert:data
completion:^(NSDictionary *result, NSError *error) {
}];
我需要使用
NSOperation
使其并发(比GCD
调度块更多,因为我需要对操作流程和取消进行更多控制)。现在,假设执行正常的完成块,我可以使用
NSBlockOperation
- (NSOperation *)executeBlock:(void (^)(void))block
inQueue:(NSOperationQueue *)queue
completion:(void (^)(BOOL finished))completion
{
NSOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:block];
NSOperation *completionOperation = [NSBlockOperation blockOperationWithBlock:^{
completion(blockOperation.isFinished);
}];
[completionOperation addDependency:blockOperation];
[[NSOperationQueue currentQueue] addOperation:completionOperation];
[queue addOperation:blockOperation];
return blockOperation;
}
所以称它为
[self executeBlock:^{
/// my sync code
} inQueue:operationQueue
completion:^(BOOL finished) {
}];
问题是那里有异步代码:
void (^completionBlock)() = ^void() {
// this is the NSOperation completion block where sync code is executed
};
和
void (^insertCompletionBlock)(NSDictionary *, NSError *) = ^void(NSDictionary *result, NSError *error) {
// this is the insert api completion block
};
所以有
[container insert:data completion:insertCompletionBlock];
如果我像这样打一个嵌套电话
[self executeBlock:^{
[container insert:data
completion:^(NSDictionary *result, NSError *error) {
}];
} inQueue:operationQueue
completion:^(BOOL finished) {
}];
该
NSOperation
将立即结束,因为insert:completion:
方法将在其调用完成后返回。那么,如何序列化此执行以便在执行
NSBlockOperation
的嵌套完成块之后调用insert:completion:
?[更新]
使用@Mozilla的解决方案
我想出了一个自定义的
NSBlockOperation
,用于在其上添加一些属性:@interface MyCloudOperation: NSBlockOperation
@property(nonatomic,strong) id result;
@property(nonatomic,strong) NSError *error;
@end
@implementation MXMCloudOperation
@end
和这个
MyCloudOperation *blockOp=[[MyCloudOperation alloc] init];
__weak MXMCloudOperation *weakBlockOp=blockOp;
[blockOp setCompletionBlock:^{
if(completion) completion(weakBlockOp.result,weakBlockOp.error);
}];
[blockOp addExecutionBlock:^{
dispatch_semaphore_t mutex = dispatch_semaphore_create(0);
void (^insertCompletionBlock)(NSDictionary *, NSError *) = ^void(NSDictionary *result, NSError *error) {
if(error) {
weakBlockOp.error=error;
NSLog(@"Error saving to %@ data\n%@", containerName,
error.localizedDescription);
} else {
weakBlockOp.result=result;
NSLog(@"Data %@ sent", result);
}
dispatch_semaphore_signal(mutex);
};
[container insert:data completion:insertCompletionBlock];
dispatch_semaphore_wait(mutex, DISPATCH_TIME_FOREVER);
}];
[operationQueue addOperation:blockOp];
我在这里不喜欢引用我的
NSBlockOperation
来传递完成处理程序的参数,但是我现在没有找到更好的解决方案。 最佳答案
我通过使用dispatch_semaphore_t
解决了这个问题。
- (void)saveWebDataInternal:(ResponseModel *)data completion:(void(^)(NSArray *))completion
{
NSBlockOperation *op = [[NSBlockOperation alloc] init];
op.completionBlock = ^{
dispatch_async(dispatch_get_main_queue(), ^{
[self loadCachedDataInternal:completion];
});
};
[op addExecutionBlock:^{
dispatch_semaphore_t mutex = dispatch_semaphore_create(0);
[self.cacheDAO asyncImport:data completion:^{
dispatch_semaphore_signal(mutex);
}];
dispatch_semaphore_wait(mutex, DISPATCH_TIME_FOREVER);
}];
// start operation
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[op start];
});
}
关于ios - 带有嵌套补全块的NSBlockOperation,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33262312/