在iOS App中使用GCD的多个calculateDirectionsWithCompletionHandler

时间:2014-04-01 12:46:07

标签: ios iphone mkmapview grand-central-dispatch

我需要对MKDirections(10)做多次请求,我希望加快我的iOS7应用。 10 calculateDirectionsWithCompletionHandler之后,我想更新用户界面。 我尝试使用GCD进行异步调用。

我的问题是,GCD我不知道如何在更新用户界面之前等待所有calculateDirectionsWithCompletionHandler的结束。

这是我的代码:

dispatch_group_t taskGroup =  dispatch_group_create();
dispatch_queue_t mainQueue =  dispatch_get_main_queue();

dispatch_group_async(taskGroup, mainQueue, ^{
    [directions1 calculateDirectionsWithCompletionHandler:
     ^(MKDirectionsResponse *response, NSError *error) {...}];
});

.........

dispatch_group_async(taskGroup, mainQueue, ^{
    [directions10 calculateDirectionsWithCompletionHandler:
     ^(MKDirectionsResponse *response, NSError *error) {...}];
});

/* Update UI */
dispatch_group_notify(taskGroup, mainQueue, ^{

    /* Do some processing here and UI Update */

});

问题是UI在所有calculateDirectionsWithCompletionHandler

结束之前更新

为什么呢? 我的代码中存在哪个问题?

感谢您的帮助。

0 个答案:

没有答案