『导言』
iOS开发中经常用到遍历数据用到for循环,其实GCD中有个更好用的方法来快速迭代,下面我们来看看两者的区别和联系。
一、代码分析(用遍历0到9的10个数字为例):
- for循环:
- 遍历10个数字
for (int i = 0; i<10; i++) {
NSLog(@"i = %d,线程 = %@",i,[NSThread currentThread]);
}
```
* 结果打印
2017-02-16 14:58:01.070 02-掌握-GCD的快速迭代-zwj[1420:97101] i = 0,线程 = <NSThread: 0x6080000654c0>{number = 1, name = main}
2017-02-16 14:58:01.071 02-掌握-GCD的快速迭代-zwj[1420:97101] i = 1,线程 = <NSThread: 0x6080000654c0>{number = 1, name = main}
2017-02-16 14:58:01.071 02-掌握-GCD的快速迭代-zwj[1420:97101] i = 2,线程 = <NSThread: 0x6080000654c0>{number = 1, name = main}
2017-02-16 14:58:01.071 02-掌握-GCD的快速迭代-zwj[1420:97101] i = 3,线程 = <NSThread: 0x6080000654c0>{number = 1, name = main}
2017-02-16 14:58:01.071 02-掌握-GCD的快速迭代-zwj[1420:97101] i = 4,线程 = <NSThread: 0x6080000654c0>{number = 1, name = main}
2017-02-16 14:58:01.071 02-掌握-GCD的快速迭代-zwj[1420:97101] i = 5,线程 = <NSThread: 0x6080000654c0>{number = 1, name = main}
2017-02-16 14:58:01.072 02-掌握-GCD的快速迭代-zwj[1420:97101] i = 6,线程 = <NSThread: 0x6080000654c0>{number = 1, name = main}
2017-02-16 14:58:01.072 02-掌握-GCD的快速迭代-zwj[1420:97101] i = 7,线程 = <NSThread: 0x6080000654c0>{number = 1, name = main}
2017-02-16 14:58:01.072 02-掌握-GCD的快速迭代-zwj[1420:97101] i = 8,线程 = <NSThread: 0x6080000654c0>{number = 1, name = main}
2017-02-16 14:58:01.072 02-掌握-GCD的快速迭代-zwj[1420:97101] i = 9,线程 = <NSThread: 0x6080000654c0>{number = 1, name = main}
* 结果分析
结果:number= 1,始终只在主线程执行,如果,遍历数据很多的话,项目运行肯定会卡。
* GCD循环:
* GCD遍历代码`dispatch_apply`
/**
@param iterations#> 遍历的次数,相当于i=10 description#>
@param queue#> 队列(必须为并发队列,串行队列无意义;也不能传主队列,因为死锁) description#>dispatch_get_global_queue
@param size_t <#size_t description#>
@return 索引index 相当于i为0到9
*/
// dispatch_apply(<#size_t iterations#>, <#dispatch_queue_t _Nonnull queue#>, <#^(size_t)block#>)
dispatch_apply(10,dispatch_get_global_queue(0, 0), ^(size_t index) {
NSLog(@"%zd = %@",index,[NSThread currentThread]);
});
* 打印结果
2017-02-16 15:10:38.602 02-掌握-GCD的快速迭代-zwj[1447:102911] 3 = <NSThread: 0x608000268e40>{number = 5, name = (null)}
2017-02-16 15:10:38.602 02-掌握-GCD的快速迭代-zwj[1447:102666] 0 = <NSThread: 0x60000007ee80>{number = 1, name = main}
2017-02-16 15:10:38.602 02-掌握-GCD的快速迭代-zwj[1447:102912] 2 = <NSThread: 0x60000026ca00>{number = 4, name = (null)}
2017-02-16 15:10:38.603 02-掌握-GCD的快速迭代-zwj[1447:102910] 1 = <NSThread: 0x60800007eac0>{number = 3, name = (null)}
2017-02-16 15:10:38.603 02-掌握-GCD的快速迭代-zwj[1447:102911] 4 = <NSThread: 0x608000268e40>{number = 5, name = (null)}
2017-02-16 15:10:38.603 02-掌握-GCD的快速迭代-zwj[1447:102666] 5 = <NSThread: 0x60000007ee80>{number = 1, name = main}
2017-02-16 15:10:38.603 02-掌握-GCD的快速迭代-zwj[1447:102912] 6 = <NSThread: 0x60000026ca00>{number = 4, name = (null)}
2017-02-16 15:10:38.603 02-掌握-GCD的快速迭代-zwj[1447:102910] 7 = <NSThread: 0x60800007eac0>{number = 3, name = (null)}
2017-02-16 15:10:38.603 02-掌握-GCD的快速迭代-zwj[1447:102911] 8 = <NSThread: 0x608000268e40>{number = 5, name = (null)}
2017-02-16 15:10:38.603 02-掌握-GCD的快速迭代-zwj[1447:102666] 9 = <NSThread: 0x60000007ee80>{number = 1, name = main}
* 结果分析
1. index(相当于for循环的i)遍历10次。打印线程的number 为1.3.4.5,其中1表示主线程,非1得为子线程,即开启了主线程和子线程来执行任务。
2.GCD的遍历为:异步函数&并发队列,必须为并发队列,串行队列无意义;也不能传主队列,因为死锁.
3.快的原因(异步的并发的队列,自动开启子线程