【问题标题】:How to wait for completion of all asynchronous tasks before running final tasks如何在运行最终任务之前等待所有异步任务完成
【发布时间】: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


    【解决方案1】:

    您的-getDataForHKQuantity:withCompletion: 方法是异步的。因此,通过您的调度组,您将 调用 同步到这些方法,而不是方法本身完成的工作。

    换句话说,您嵌套了两个异步调用,但通过调度组仅同步第一级。

    您需要想出一个不同的策略来控制您的程序流程。

    两个例子:

    1。使用信号量(阻塞)

    前段时间,我将信号量用于类似的任务,不确定这是最好的策略,但在你的情况下,它会像这样:

    semaphore = dispatch_semaphore_create(0);
    
    for( NSString *hkType in typesToRetrieve)
    {
        [self getDataForHKQuantity: hkType withCompletion:^(NSArray *results) {
    
            // register running method here
    
            [self processResultsArray:results];
    
            if (isLastMethod) // need to keep track of concurrent methods running
            {
                dispatch_semaphore_signal(semaphore);
            }
        }];
    }
    
    // your program will wait here until all calls to getDataForHKQuantity complete
    // so you could run the whole thing in a background thread and wait for it to finish
    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
    

    2。使用 dispatch_group

    dispatch_group_t serviceGroup = dispatch_group_create();
    
    for( NSString *hkType in typesToRetrieve)
    {
        dispatch_group_enter(serviceGroup);
    
        [self getDataForHKQuantity: hkType withCompletion:^(NSArray *results) {
    
            [self processResultsArray:results];
            dispatch_group_leave(serviceGroup);
        }];
    }
    
    dispatch_group_notify(serviceGroup,dispatch_get_main_queue(),^{
        // Won't get here until everything has finished
    });
    

    还可以查看this link 了解更多信息。

    【讨论】:

    • 我不会使用信号量,因为它们会阻塞并且您可能在主线程上。你仍然可以使用 dispatch_group,但你必须使用 dispatch_group_enter() 和 dispatch_group_leave()。你必须在异步调用之前进入,然后在竞争块中离开。
    猜你喜欢
    • 2016-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多