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的关联API 12 * 该函数按指定的次数将指定的Block追加到指定的Dispatch Queue中,并等到全部的处理执行结束 13 * 14 * @param 10 指定重复次数 指定10次 15 * @param queue 追加对象的Dispatch Queue 16 * @param index 带有参数的Block, index的作用是为了按执行的顺序区分各个Block 17 * 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] 0 28 2016-02-25 19:24:39.102 dispatch_apply测试[2985:165086] 1 29 2016-02-25 19:24:39.104 dispatch_apply测试[2985:165004] 4 30 2016-02-25 19:24:39.104 dispatch_apply测试[2985:165004] 5 31 2016-02-25 19:24:39.104 dispatch_apply测试[2985:165004] 6 32 2016-02-25 19:24:39.103 dispatch_apply测试[2985:165088] 3 33 2016-02-25 19:24:39.104 dispatch_apply测试[2985:165004] 7 34 2016-02-25 19:24:39.105 dispatch_apply测试[2985:165004] 8 35 2016-02-25 19:24:39.105 dispatch_apply测试[2985:165004] 9 36 2016-02-25 19:24:39.102 dispatch_apply测试[2985:165087] 2 37 2016-02-25 19:24:39.105 dispatch_apply测试[2985:165004] done 38 * !!!因为在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: a 20 2016-02-25 19:37:17.308 dispatch_apply测试[3010:167956] 1: b 21 2016-02-25 19:37:17.308 dispatch_apply测试[3010:167957] 3: d 22 2016-02-25 19:37:17.308 dispatch_apply测试[3010:167871] 4: e 23 2016-02-25 19:37:17.309 dispatch_apply测试[3010:167957] 6: g 24 2016-02-25 19:37:17.309 dispatch_apply测试[3010:167871] 7: h 25 2016-02-25 19:37:17.309 dispatch_apply测试[3010:167957] 8: i 26 2016-02-25 19:37:17.309 dispatch_apply测试[3010:167871] 9: j 27 2016-02-25 19:37:17.308 dispatch_apply测试[3010:167956] 5: f 28 2016-02-25 19:37:17.308 dispatch_apply测试[3010:167955] 2: c 29 * !!!因为在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: d 24 2016-02-25 19:49:53.189 dispatch_apply测试[3060:171852] 1: b 25 2016-02-25 19:49:53.189 dispatch_apply测试[3060:171853] 2: c 26 2016-02-25 19:49:53.189 dispatch_apply测试[3060:171850] 0: a 27 2016-02-25 19:49:53.189 dispatch_apply测试[3060:171856] 4: e 28 2016-02-25 19:49:53.189 dispatch_apply测试[3060:171852] 5: f 29 2016-02-25 19:49:53.190 dispatch_apply测试[3060:171853] 6: g 30 2016-02-25 19:49:53.190 dispatch_apply测试[3060:171850] 7: h 31 2016-02-25 19:49:53.190 dispatch_apply测试[3060:171852] 9: j 32 2016-02-25 19:49:53.190 dispatch_apply测试[3060:171856] 8: i 33 2016-02-25 19:49:53.218 dispatch_apply测试[3060:171760] 回到主线程执行用户界面更新等操作 34 * 35 */ 36 }