假设有一个带有完成块的方法来执行:
[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
来传递完成处理程序的参数,但我现在还没有找到更好的解决方案。
答案 0 :(得分:3)
我使用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];
});
}