这看起来应该很简单,但我遇到了很多麻烦。我有一个函数可以触发一些在后台运行并具有完成块的其他函数。我希望我的函数等到所有完成块都在它返回之前被调用。
我无法控制我在后台执行的函数。否则我只是修改它以使用dispatch_async和我自己的队列,然后等待该队列完成。 / p>
我的情况示例:
- (void)functionThatShouldBeSynchronous {
for (int i = 0; i < 10; i++) {
[self doSomethingInBackground:^{
NSLog(@"completed!");
}];
}
// How do I wait until all 10 threads have completed before returning?
}
- (void)doSomethingInBackground:(void(^)())completion {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
[NSThread sleepForTimeInterval:1.0f]; // Do stuff
completion(); // Execute completion block
});
}
提前致谢。
答案 0 :(得分:28)
使用这样的调度组:
- (void)functionThatShouldBeSynchronous {
dispatch_group_t taskGroup = dispatch_group_create();
for (int i = 0; i < 10; i++) {
dispatch_group_enter(taskGroup);
[self doSomethingInBackground:^{
NSLog(@"completed!");
dispatch_group_leave(taskGroup);
}];
}
// Waiting for threads
dispatch_group_wait(taskGroup, DISPATCH_TIME_FOREVER);
dispatch_release(taskGroup);
// Background work complete
}
如果您希望超时等待线程,可以将dispatch_group_wait行更改为此
// Waiting 10 seconds before giving up
if (dispatch_group_wait(taskGroup, dispatch_time(DISPATCH_TIME_NOW, 10000000000)) != 0) {
// Timeout
}
参数以纳秒为单位。
正如bbum所说,你不应该阻止主线程。在这种情况下,你可以这样做:
typedef void(^MyCompletionHandler)();
-(void)functionDoingBackgroundWorkWithCompletionHandler:(MyCompletionHandler)completionHandler {
dispatch_group_t taskGroup = dispatch_group_create();
for (int i = 0; i < 10; i++) {
dispatch_group_enter(taskGroup);
[self doSomethingInBackground:^{
NSLog(@"completed!");
dispatch_group_leave(taskGroup);
}];
}
dispatch_queue_t waitingQueue = dispatch_queue_create("com.mycompany.myapp.waitingQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(waitingQueue, ^{
// Waiting for threads
dispatch_group_wait(taskGroup, DISPATCH_TIME_FOREVER);
dispatch_release(taskGroup);
// Background work complete
dispatch_async(dispatch_get_main_queue(), ^{
// Calling the completion handler on the main thread (If you like)
completionHandler();
});
dispatch_release(waitingQueue);
});
}