我的建议是使用dispatch_group
。致电dispatch_async
之前致电dispatch_group_enter
,并致电dispatch_group_leave
,并由dispatch_async
执行该块的结尾。然后,在将所有块排入队列之后,使用dispatch_group_notify
安排完成块,该块将在所有其他dispatch_async
块完成后运行。
dispatch_group_t group = dispatch_group_create();
for (...) {
dispatch_group_enter(group);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
...
dispatch_group_leave(group);
});
}
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
// This gets called when all the other blocks have finished
});
或者,你可以使用一个NSOperationQueue
代替libdispatch,并完成操作,其中列出所有其他操作的依赖。但是,这有一个缺点,即完成操作不会在主队列上执行。
NSOperation *completionOperation = [NSBlockOperation blockOperationWithBlock:^{
// This gets called when all the other operations have finished
}];
for (...) {
NSOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
...
}];
[completionOperation addDependency:operation];
[operationQueue addOperation:operation];
}
[operationQueue addOperation:completionOperation];
你想等待最后一次做什么呼叫或你是什么意思。我害怕我根本没有得到它 –
[在布局子视图中实现Cocoa Touch中的去抖整合模式](https://stackoverflow.com/q/28153632) –