Using GCD to Wait on Many Tasks

本文介绍如何利用Grand Central Dispatch (GCD) 中的dispatch_groups特性来处理未知数量的异步HTTP请求,并确保所有请求完成后才继续执行后续操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自出处:http://amro.tumblr.com/post/48248949039/using-gcd-to-wait-on-many-tasks


amro's blog

  • Using GCD to Wait on Many Tasks

    I recently needed a way to perform an unknown number of asynchronous http requests and wait until they were all done before proceeding. dispatch_groups are a neat feature of Grand Central Dispatch (GCD) that made this easy to do.

    There are a couple of ways to use dispatch_groups but the basic idea is the same: create a dispatch_group, give it some tasks, and wait for it to finish those tasks.

    First, let’s create a group:

    dispatch_group_t group = dispatch_group_create();

    There are two ways to add tasks to our dispatch_group. You can either call dispatch_group_async with the group, a dispatch_queue and block to run or manually call dispatch_group_enter.

    Calling dispatch_group_async looks something like this:

    dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // Do stuff on a global background queue here
    });

    Or you can manage tasks manually by calling dispatch_group_enter and dispatch_group_leave in pairs:

    dispatch_group_enter(group);
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // Do stuff on a global background queue here
        dispatch_group_leave(group);
    });

    Using dispatch_group_enter/dispatch_group_leave is handy when you’re using libraries that provide asynchronous operation (e.g. AFNetworking).

    The last thing to do is wait for all of the tasks to finish. The preferred way to do this is to use dispatch_group_notify:

    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        // Do stuff after all tasks are finished
    });

    dispatch_group_notify is nice because it does not block the thread it’s called from. If you have a reason to block the current thread, use dispatch_group_wait instead:

    dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
    // Do stuff after all tasks are finished

    A full example looks like this:

    dispatch_group_t group = dispatch_group_create();
    
    dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // Do stuff on a global background queue here
    });
    
    dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // Do more stuff on a global background queue here
    });
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        // Do stuff after all tasks are finished
    });

    One important note about dispatch objects: If your project’s deployment target is less than 6.0, you need to manage memory with dispatch_retain and dispatch_release for any dispatch objects you create (groups, queues, semaphores). ARC takes care of managing memory for dispatch objects when your deployment target is 6.0+ (10.8+ for OS X).




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值