iOS多线程GCD深入之Dispatch Group介绍

iOS多线程GCD深入之Dispatch Group介绍    -----   承接  iOS多线程GCD入门

多线程任务,在多个任务全部结束后执行某一任务:

 若是 Serial Dispatch Queue ,只要将想执行的任务追加到尾部即可。

 若是 Concurrent Dispatch Queue ,则需要用到 Dispatch Group。

这篇文章主要是介绍,使用例子见文章结尾链接

一.开头例子 

   以一个简单例子为引:

   //Dispatch Group
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_t group = dispatch_group_create();
    
    dispatch_group_async(group, queue, ^{NSLog(@"fun 1");});
    dispatch_group_async(group, queue, ^{NSLog(@"fun 2");});
    dispatch_group_async(group, queue, ^{NSLog(@"fun 3");});
    
    dispatch_group_notify(group, dispatch_get_main_queue(),^{NSLog(@"done")};
    输出结果为:

2017-02-13 15:24:08.201 UIKitLearn[4697:167578] fun 1
2017-02-13 15:24:08.202 UIKitLearn[4697:167594] fun 2
2017-02-13 15:24:08.202 UIKitLearn[4697:167580] fun 3
2017-02-13 15:24:08.202 UIKitLearn[4697:167580] done

    这里我们创建一个并行队列 queue 和一个队列组 group,用到函数 dispatch_group_async 将任务加入队列,并将队列加入到group中,函数 dispatch_group_notify() 意为在 group 中队列的任务都完成后,执行 notify 中 block 的代码,所以在最后输出了 “done”。让输出fun 1,fun 2,fun 3的代码同输出done的代码产生了同步的效果,要在三个块都结束后done才执行。

    

二.分析

官方介绍

1.dispatch_group_t

Discussion

  A dispatch group is a mechanism for monitoring a set of blocks. Your application can monitor the blocks in the group synchronously or asynchronously depending on your needs. By extension, a group can be useful for synchronizing for code that depends on the completion of other tasks.

  Note that the blocks in a group may be run on different queues, and each individual block can add more blocks to the group. 

  The dispatch group keeps track of how many blocks are outstanding, and GCD retains the group until all its associated blocks complete execution.

翻译:

   一个dispatch group是一个用来监视一个blocks集合的机制。你的应用可以根据你的需求同步或异步的监视组内的blocks。相关地,一个group可以用来基于其它的任务完成后同步执行代码(日,这句好难)。

    注意的是,在group中的blocks也许会运行在不同的队列中,每个独立的block可以加更多的blocks到group中。

    Dispatch group 保持追踪有多少blocks没有解决,并且GCD持有这个group,直到所有它关联的blocks执行完成。

分析:

  那么一个group就像是一个调度组,仿佛可以比较容易的同步异步控制一些任务,但是对于可以使用哪种队列,有什么效果,以及整个生命周期,还是不太清晰。


2.disptach_group_async(dispatch_group_t group, dispatch_queue_t queue, dispatch_block_t block);

Parameters:

 group

  A dispatch group to associate the submitted block object with. The group is retained by the system until the block has run to completion. This parameter cannot be NULL.

 queue

  The dispatch queue to which the block object is submitted for asynchronous invocation. The queue is retained by the system until the block has run to completion. This parameter cannot be NULL.

 block

  The block object to perform asynchronously. This function performs a Block_copy and Block_release on behalf of the caller.

Discussion:
  Submits a block to a dispatch queue and associates the block object with the given dispatch group. The dispatch group can be used to wait for the completion of the block objects it references.

翻译:(我就按大体意思翻译了)group这个参数会被系统持有,用来关联blocks,都执行完了,才被释放。


3.disptach_group_notify(dispatch_group_t group, dispatch_queue_t queue, dispatch_block_t block);

Discussion:

  This function schedules a notification block to be submitted to the specified queue when all blocks associated with the dispatch group have completed. If the group is empty (no block objects are associated with the dispatch group), the notification block object is submitted immediately.

  When the notification block is submitted, the group is empty. The group can either be released with dispatch_release or be reused for additional block objects. See dispatch_group_async for more information.

翻译:大体意思是,group像是一个调度中心,在group种的其它任务都执行完成后,这个block会在queue中执行。


4.disptach_group_enter(dispatch_group_t group) & disptach_group_leave(dispatch_group_t group)

Discussion:

  Calling this function increments the current count of outstanding tasks in the group. Using this function (with dispatch_group_leave) allows your application to properly manage the task reference count if it explicitly adds and removes tasks from the group by a means other than using the dispatch_group_async function. A call to this function must be balanced with a call to dispatch_group_leave. You can use this function to associate a block with more than one group at the same time.

翻译:enter函数和leave函数要成对出现,这两个函数的作用是追加block到group中,比如用于多次网络请求。


暂时介绍这些,详细使用请点链接:iOS多线程GCD深入之Dispatch Group使用





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值