8

这似乎应该很简单,但我遇到了很多麻烦。我有一个函数可以触发一堆在后台运行并具有完成块的其他函数。我希望我的函数等到所有完成块都被调用后才返回。

我无法控制我正在调用的在后台执行的函数。否则我只会修改它以将 dispatch_async 与我自己的队列一起使用,然后等待该队列完成。

我的情况示例:

- (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
    });
}

提前致谢。

4

1 回答 1

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);
    });
}
于 2013-05-12T23:32:39.230 回答