你的位置:首页 > 操作系统

[操作系统]GCD中的dispatch_apply的用法及作用

GCD中的dispatch_apply的用法及作用

(一)dispatch_apply的基本用法

dispatch_apply函数是dispatch_sync函数和Dispatch Group的关联API,该函数按指定的次数将指定的Block追加到指定的Dispatch Queue中,并等到全部的处理执行结束

 1 /*! 2  * @brief dispatch_apply的用法 3 */ 4 - (void)dispatchApplyTest1 { 5   //生成全局队列 6   dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 7    8    9   /*! dispatch_apply函数说明10    *11    * @brief dispatch_apply函数是dispatch_sync函数和Dispatch Group的关联API12    *     该函数按指定的次数将指定的Block追加到指定的Dispatch Queue中,并等到全部的处理执行结束13    *14    * @param 10  指定重复次数 指定10次15    * @param queue 追加对象的Dispatch Queue16    * @param index 带有参数的Block, index的作用是为了按执行的顺序区分各个Block17    *18   */19   dispatch_apply(10, queue, ^(size_t index) {20     NSLog(@"%zu", index);21   });22   NSLog(@"done");23   24   /*!25    * @brief 输出结果26    *27    2016-02-25 19:24:39.102 dispatch_apply测试[2985:165004] 028    2016-02-25 19:24:39.102 dispatch_apply测试[2985:165086] 129    2016-02-25 19:24:39.104 dispatch_apply测试[2985:165004] 430    2016-02-25 19:24:39.104 dispatch_apply测试[2985:165004] 531    2016-02-25 19:24:39.104 dispatch_apply测试[2985:165004] 632    2016-02-25 19:24:39.103 dispatch_apply测试[2985:165088] 333    2016-02-25 19:24:39.104 dispatch_apply测试[2985:165004] 734    2016-02-25 19:24:39.105 dispatch_apply测试[2985:165004] 835    2016-02-25 19:24:39.105 dispatch_apply测试[2985:165004] 936    2016-02-25 19:24:39.102 dispatch_apply测试[2985:165087] 237    2016-02-25 19:24:39.105 dispatch_apply测试[2985:165004] done38    * !!!因为在Global Dispatch Queue中执行,所以各个处理的执行时间不定39    但done一定会输出在最后的位置,因为dispatch_apply函数会等待所以的处理结束40   */41 }

 

(二)dispatch_apply的使用技巧:模拟for循环

 1 /*! 2  * @brief 实例:当要对NSArray类对象的所有元素执行处理时,不必一个一个的编写for循环部分 3 */ 4 - (void)dispatchApplyTest2 { 5   //1.创建NSArray类对象 6   NSArray *array = @[@"a", @"b", @"c", @"d", @"e", @"f", @"g", @"h", @"i", @"j"]; 7    8   //2.创建一个全局队列 9   dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);10   11   //3.通过dispatch_apply函数对NSArray中的全部元素进行处理,并等待处理完成,12   dispatch_apply([array count], queue, ^(size_t index) {13     NSLog(@"%zu: %@", index, [array objectAtIndex:index]);14   });15   NSLog(@"done");16   /*!17    * @brief 输出结果18    *19    2016-02-25 19:37:17.308 dispatch_apply测试[3010:167871] 0: a20    2016-02-25 19:37:17.308 dispatch_apply测试[3010:167956] 1: b21    2016-02-25 19:37:17.308 dispatch_apply测试[3010:167957] 3: d22    2016-02-25 19:37:17.308 dispatch_apply测试[3010:167871] 4: e23    2016-02-25 19:37:17.309 dispatch_apply测试[3010:167957] 6: g24    2016-02-25 19:37:17.309 dispatch_apply测试[3010:167871] 7: h25    2016-02-25 19:37:17.309 dispatch_apply测试[3010:167957] 8: i26    2016-02-25 19:37:17.309 dispatch_apply测试[3010:167871] 9: j27    2016-02-25 19:37:17.308 dispatch_apply测试[3010:167956] 5: f28    2016-02-25 19:37:17.308 dispatch_apply测试[3010:167955] 2: c29    * !!!因为在Global Dispatch Queue中执行,所以各个处理的执行时间不定30     但done一定会输出在最后的位置,因为dispatch_apply函数会等待所以的处理结束31   */32 }

(三)在dispatch_async函数中异步执行dispatch_apply函数,模拟dispatch_sync的同步效果

 1 /*! 2  * @brief 推荐在dispatch_async函数中异步执行dispatch_apply函数 3   效果   dispatch_apply函数与dispatch_sync函数形同,会等待处理执行结束 4 */ 5 - (void)dispatchApplyTest3 { 6   NSArray *array = @[@"a", @"b", @"c", @"d", @"e", @"f", @"g", @"h", @"i", @"j"]; 7   dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 8    9   dispatch_async(queue, ^{10     11     dispatch_apply([array count], queue, ^(size_t index) {12       NSLog(@"%zu: %@", index, [array objectAtIndex:index]);13     });14     15     dispatch_async(dispatch_get_main_queue(), ^{16       NSLog(@"回到主线程执行用户界面更新等操作");17     });18     19   });20   /*!21    * @brief 执行结果22    *23    2016-02-25 19:49:53.189 dispatch_apply测试[3060:171856] 3: d24    2016-02-25 19:49:53.189 dispatch_apply测试[3060:171852] 1: b25    2016-02-25 19:49:53.189 dispatch_apply测试[3060:171853] 2: c26    2016-02-25 19:49:53.189 dispatch_apply测试[3060:171850] 0: a27    2016-02-25 19:49:53.189 dispatch_apply测试[3060:171856] 4: e28    2016-02-25 19:49:53.189 dispatch_apply测试[3060:171852] 5: f29    2016-02-25 19:49:53.190 dispatch_apply测试[3060:171853] 6: g30    2016-02-25 19:49:53.190 dispatch_apply测试[3060:171850] 7: h31    2016-02-25 19:49:53.190 dispatch_apply测试[3060:171852] 9: j32    2016-02-25 19:49:53.190 dispatch_apply测试[3060:171856] 8: i33    2016-02-25 19:49:53.218 dispatch_apply测试[3060:171760] 回到主线程执行用户界面更新等操作34    *35   */36 }