GCD中的dispatch_apply的用法及作用
(一)dispatch_apply的基本用法
/*!
* @brief dispatch_apply的用法
*/
- (void)dispatchApplyTest1 {
//生成全局队列
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, ); /*! dispatch_apply函数说明
*
* @brief dispatch_apply函数是dispatch_sync函数和Dispatch Group的关联API
* 该函数按指定的次数将指定的Block追加到指定的Dispatch Queue中,并等到全部的处理执行结束
*
* @param 10 指定重复次数 指定10次
* @param queue 追加对象的Dispatch Queue
* @param index 带有参数的Block, index的作用是为了按执行的顺序区分各个Block
*
*/
dispatch_apply(, queue, ^(size_t index) {
NSLog(@"%zu", index);
});
NSLog(@"done"); /*!
* @brief 输出结果
*
2016-02-25 19:24:39.102 dispatch_apply测试[2985:165004] 0
2016-02-25 19:24:39.102 dispatch_apply测试[2985:165086] 1
2016-02-25 19:24:39.104 dispatch_apply测试[2985:165004] 4
2016-02-25 19:24:39.104 dispatch_apply测试[2985:165004] 5
2016-02-25 19:24:39.104 dispatch_apply测试[2985:165004] 6
2016-02-25 19:24:39.103 dispatch_apply测试[2985:165088] 3
2016-02-25 19:24:39.104 dispatch_apply测试[2985:165004] 7
2016-02-25 19:24:39.105 dispatch_apply测试[2985:165004] 8
2016-02-25 19:24:39.105 dispatch_apply测试[2985:165004] 9
2016-02-25 19:24:39.102 dispatch_apply测试[2985:165087] 2
2016-02-25 19:24:39.105 dispatch_apply测试[2985:165004] done
* !!!因为在Global Dispatch Queue中执行,所以各个处理的执行时间不定
但done一定会输出在最后的位置,因为dispatch_apply函数会等待所以的处理结束
*/
}
(二)dispatch_apply的使用技巧:模拟for循环
/*!
* @brief 实例:当要对NSArray类对象的所有元素执行处理时,不必一个一个的编写for循环部分
*/
- (void)dispatchApplyTest2 {
//1.创建NSArray类对象
NSArray *array = @[@"a", @"b", @"c", @"d", @"e", @"f", @"g", @"h", @"i", @"j"]; //2.创建一个全局队列
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, ); //3.通过dispatch_apply函数对NSArray中的全部元素进行处理,并等待处理完成,
dispatch_apply([array count], queue, ^(size_t index) {
NSLog(@"%zu: %@", index, [array objectAtIndex:index]);
});
NSLog(@"done");
/*!
* @brief 输出结果
*
2016-02-25 19:37:17.308 dispatch_apply测试[3010:167871] 0: a
2016-02-25 19:37:17.308 dispatch_apply测试[3010:167956] 1: b
2016-02-25 19:37:17.308 dispatch_apply测试[3010:167957] 3: d
2016-02-25 19:37:17.308 dispatch_apply测试[3010:167871] 4: e
2016-02-25 19:37:17.309 dispatch_apply测试[3010:167957] 6: g
2016-02-25 19:37:17.309 dispatch_apply测试[3010:167871] 7: h
2016-02-25 19:37:17.309 dispatch_apply测试[3010:167957] 8: i
2016-02-25 19:37:17.309 dispatch_apply测试[3010:167871] 9: j
2016-02-25 19:37:17.308 dispatch_apply测试[3010:167956] 5: f
2016-02-25 19:37:17.308 dispatch_apply测试[3010:167955] 2: c
* !!!因为在Global Dispatch Queue中执行,所以各个处理的执行时间不定
但done一定会输出在最后的位置,因为dispatch_apply函数会等待所以的处理结束
*/
}
(三)在dispatch_async函数中异步执行dispatch_apply函数,模拟dispatch_sync的同步效果
/*!
* @brief 推荐在dispatch_async函数中异步执行dispatch_apply函数
效果 dispatch_apply函数与dispatch_sync函数形同,会等待处理执行结束
*/
- (void)dispatchApplyTest3 {
NSArray *array = @[@"a", @"b", @"c", @"d", @"e", @"f", @"g", @"h", @"i", @"j"];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, ); dispatch_async(queue, ^{ dispatch_apply([array count], queue, ^(size_t index) {
NSLog(@"%zu: %@", index, [array objectAtIndex:index]);
}); dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"回到主线程执行用户界面更新等操作");
}); });
/*!
* @brief 执行结果
*
2016-02-25 19:49:53.189 dispatch_apply测试[3060:171856] 3: d
2016-02-25 19:49:53.189 dispatch_apply测试[3060:171852] 1: b
2016-02-25 19:49:53.189 dispatch_apply测试[3060:171853] 2: c
2016-02-25 19:49:53.189 dispatch_apply测试[3060:171850] 0: a
2016-02-25 19:49:53.189 dispatch_apply测试[3060:171856] 4: e
2016-02-25 19:49:53.189 dispatch_apply测试[3060:171852] 5: f
2016-02-25 19:49:53.190 dispatch_apply测试[3060:171853] 6: g
2016-02-25 19:49:53.190 dispatch_apply测试[3060:171850] 7: h
2016-02-25 19:49:53.190 dispatch_apply测试[3060:171852] 9: j
2016-02-25 19:49:53.190 dispatch_apply测试[3060:171856] 8: i
2016-02-25 19:49:53.218 dispatch_apply测试[3060:171760] 回到主线程执行用户界面更新等操作
*
*/
}