版权声明:知识版权是属于全人类的! 欢迎评论与转载!!! https://blog.csdn.net/zhuge1127/article/details/82464766
重复执行某个任务,但是注意这个方法没有办法异步执行
(为了不阻塞线程可以使用dispatch_async()包装一下再执行
* @abstract
* Submits a block to a dispatch queue for parallel invocation.
# 提交一个Block到队列并发调用
* @discussion
* Submits a block to a dispatch queue for parallel invocation. This function
* waits for the task block to complete before returning. If the specified queue
* is concurrent, the block may be invoked concurrently, and it must therefore
* be reentrant safe.
*
* Each invocation of the block will be passed the current index of iteration.
*
* @param iterations The number of iterations(反复) to perform.
*
* @param queue
* The dispatch queue to which the block is submitted.
* The preferred value to pass is DISPATCH_APPLY_AUTO to automatically use
* a queue appropriate for the calling thread.
void dispatch_apply(size_t iterations, dispatch_queue_t queue, DISPATCH_NOESCAPE void (^block)(size_t));
demo
dispatch_apply(3, dispatch_queue_create("d", NULL), ^(size_t index) {
NSLog(@"copy-%ld", index);
});
2016-11-05 17:01:38.138 Multithreading[16124:481707] copy-0
2016-11-05 17:01:38.138 Multithreading[16124:481707] copy-1
2016-11-05 17:01:38.138 Multithreading[16124:481707] copy-2
dispatch_apply(3, dispatch_queue_create("c", DISPATCH_QUEUE_CONCURRENT), ^(size_t index) {
NSLog(@"copy-%ld", index);
});
2016-11-05 17:04:10.798 Multithreading[16153:483197] copy-2
2016-11-05 17:04:10.798 Multithreading[16153:483181] copy-1
2016-11-05 17:04:10.798 Multithreading[16153:483141] copy-0