/////-------------注意----------------
这篇文章是在Swift 1.0时代写的,已经不适合当前的语法。关于Swift最新版本的GCD,参见我的这篇博客
/////-------------注意----------------
func dispatch_group_create() -> dispatch_group_t!
func dispatch_group_async(_ group: dispatch_group_t!, _ queue: dispatch_queue_t!, _ block: dispatch_block_t!)
func dispatch_group_enter(_ group: dispatch_group_t!) func dispatch_group_leave(_ group: dispatch_group_t!)这两个方法显示的讲任务组中的任务未执行完毕的任务数目加减1,这种方式用在不使用dispatch_group_async来提交任务,注意:这两个函数要配合使用,有enter要有leave,这样才能保证功能完整实现。也可以用这对函数来让一个闭包关联多个Group
func dispatch_group_notify(_ group: dispatch_group_t!, _ queue: dispatch_queue_t!, _ block: dispatch_block_t!)参数: group 监听的任务组
long dispatch_group_wait ( dispatch_group_t group, dispatch_time_t timeout );
class ViewController: UIViewController{ override func viewDidLoad(){ super.viewDidLoad() var hwcGroup = dispatch_group_create()//创建group var globalQueueDefault = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)//全局队列,这个队列为并行队列 var userCreateQueue = dispatch_queue_create("com.test.helloHwc",DISPATCH_QUEUE_SERIAL)//创建一个用户队列,这个队列为串行队列 downLoadTask1(group: hwcGroup,queue: globalQueueDefault) downLoadTask2(group: hwcGroup,queue: userCreateQueue) downLoadTask3(group: hwcGroup,queue: userCreateQueue) //letresult = dispatch_group_wait(hwcGroup,DISPATCH_TIME_FOREVER) dispatch_group_notify(hwcGroup,dispatch_get_main_queue()){ NSLog("Group tasks are done") } println("Now viewDidLoad is done") } func downLoadTask1(#group:dispatch_group_t,queue:dispatch_queue_t){ dispatch_group_async(group,queue){ sleep(3) NSLog("Task1 is done") } } func downLoadTask2(#group:dispatch_group_t,queue:dispatch_queue_t){ dispatch_group_async(group,queue){ sleep(3) NSLog("Task2 is done") } } func downLoadTask3(#group:dispatch_group_t,queue:dispatch_queue_t){ dispatch_group_async(group,queue){ sleep(3) NSLog("Task3 is done") } } override func didReceiveMemoryWarning(){ super.didReceiveMemoryWarning() } }
可以看到输出为
Now viewDidLoad is done Task2 is done Task1 is done Task3 is done Group task is done
这里task1提交到全局队列中,task2和task3提交到用户穿件的串行队列中,所以task1和task2同时输出,task3在task2结束两秒后输出。
如果把注释那行取消,会等待一段时间,再返回,读者可以自己去试验下
三、关于如何使用dispatch_group_enter/dispatch_group_leave如何使用
<pre name="code" class="plain">class ViewController: UIViewController{ override func viewDidLoad(){ super.viewDidLoad() var hwcGroup = dispatch_group_create()//创建group var globalQueueDefault = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)//全局队列,这个队列为并行队列 for index:UInt32 in 1...3{ dispatch_group_enter(hwcGroup)//提交了一个任务,任务数目加1 manualDownLoad(index){ println("Task\(index) is done") dispatch_group_leave(hwcGroup)//完成一个任务,任务数目减1 } } let result = dispatch_group_wait(hwcGroup,DISPATCH_TIME_FOREVER)//等待直到完成 dispatch_group_notify(hwcGroup,dispatch_get_main_queue()){ println("Group tasks are done") } println("Now viewDidLoad is done") } func manualDownLoad(num:UInt32,block:()->()){ println("Downloading task\(num)") sleep(num) block() } override func didReceiveMemoryWarning(){ super.didReceiveMemoryWarning() } }
输出
Downloading task1 Task1 is done Downloading task2 Task2 is done Downloading task3 Task3 is done Now viewDidLoad is done Group is done
可以看到,同步进行了下载,这里的任务是串行的,实际消耗时间9s。当然,也可以把下载的任务提交到异步队列中
class ViewController: UIViewController{ override func viewDidLoad(){ super.viewDidLoad() var hwcGroup = dispatch_group_create()//创建group var globalQueueDefault = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)//全局队列,这个队列为并行队列 for index:UInt32 in 1...3{ dispatch_group_enter(hwcGroup)//提交了一个任务,任务数目加1 manualDownLoad(index,queue:globalQueueDefault){ NSLog("Task\(index) is done") dispatch_group_leave(hwcGroup)//完成一个任务,任务数目减1 } } dispatch_group_notify(hwcGroup,dispatch_get_main_queue()){ NSLog("Group tasks are done") } NSLog("Now viewDidLoad is done") } func manualDownLoad(num:UInt32,queue:dispatch_queue_t, block:()->()){ dispatch_async(queue){ NSLog("Downloading task\(num)") sleep(num) block() } } override func didReceiveMemoryWarning(){ super.didReceiveMemoryWarning() } }
这样的话,输出是
Downloading task1 Downloading task2 Downloading task3 Now viewDidLoad is done Task1 is done Task2 is done Task3 is done
由于三个模拟的下载任务是并行的,所以实际消耗时间3s