我有两个想要同时执行的轻量级网络请求,然后当两者都完成后,调用一个块函数。
我创建的方法如下:
- (void)loadWithCompletion:(void (^)())completion
{
dispatch_semaphore_t customerSemaphore = dispatch_semaphore_create(0);
dispatch_semaphore_t communitySemaphore = dispatch_semaphore_create(0);
dispatch_async(dispatch_queue_create("mp.session.loader", DISPATCH_QUEUE_CONCURRENT), ^(void)
{
[_customerClient loadCustomerDetailsWithSuccess:^(MPCustomer* customer)
{
[self setCurrentCustomer:customer];
dispatch_semaphore_signal(customerSemaphore);
} error:^(NSError* error)
{
LogDebug(@"Got unexpected error loading customer details: %@", error);
}];
[_customerClient loadCommunityDetailsWithSuccess:^(MPCommunity* community)
{
[self setCurrentCommunity:community];
dispatch_semaphore_signal(communitySemaphore);
} error:^(NSError* error)
{
LogDebug(@"Got unexpected error loading customer details: %@", error);
}];
});
dispatch_semaphore_wait(customerSemaphore, DISPATCH_TIME_FOREVER);
dispatch_semaphore_wait(communitySemaphore, DISPATCH_TIME_FOREVER);
if (completion)
{
completion();
}
}
. . 它最终会永远等待。我看到我的两个网络请求启动了,但是我从来没有调用来自两个客户端调用的两个回调,因此两个信号量都没有发出信号。
为什么?