【发布时间】:2016-03-23 06:56:36
【问题描述】:
我一定是误解了dispatch_group,因为我的dispatch_group_notify 调用是在各个dispatch_group_async 块内进行的异步调用结束之前运行的。这是我的代码:
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_t dispatchGroup = dispatch_group_create();
// create operation for each HKTypeIdentifier for which we want to retrieve information
for( NSString *hkType in typesToRetrieve){
dispatch_group_async(dispatchGroup, queue, ^{
// this method runs several HK queries each with a completion block as indicated below
[self getDataForHKQuantity: hkType withCompletion:^(NSArray *results) {
// this completion blocks runs asynchronously as HK query completion block
// I want to runCompletionBlock only after
// all these processResultsArray calls have finished
[self processResultsArray:results];
}];
});
}
dispatch_group_notify(dispatchGroup, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self runCompletionCheck];
});
方法getDataForHKQuantity 反过来使用完成块运行对 HealthKit 的异步查询。在 HealthKit 查询的所有这些完成块运行之后,我需要运行 runCompletionCheck,但现在发生的是 runCompletionCheck 在查询完成块中的代码运行之前运行。对我来说,这意味着 dispatch_group_notify 和 dispatch_group_async 不能按我需要的方式工作,那么我做错了什么或者处理这个问题的最佳方法是什么?
总体目标:对 HealthKit 进行一系列并发查询,运行它们的完成块,然后当所有这些完成块运行时,运行最终方法。
问题有两个方面。 首先,健康包查询并不总是运行它们的完成块。我从使用计数器系统开始,在健康工具包查询的完成块中有一个计数器。这就是告诉我这些完成块并不总是运行的原因。 第二,我不知道我要运行多少个查询,因为这取决于用户拥有哪些数据源。
那么,问题是,我如何才能等到一系列健康工具包查询中的所有完成块都运行完毕后再运行最终方法?
【问题讨论】:
标签: objective-c asynchronous healthkit