一、dispatch_group
把一组任务提交到队列中,这些队列可以不相关,然后坚挺这组任务完成的事件。
几个用到的函数
1、dispatch_group_create创建一个调度任务组
func dispatch_group_create() -> dispatch_group_t!
2、dispatch_group_async 把一个任务异步提交到任务组里
func dispatch_group_async(_ group: dispatch_group_t!,
_ queue: dispatch_queue_t!,
_ block: dispatch_block_t!)
参数: group 提交到的任务组,这个任务组的对象会一直持续到任务组执行完毕
queue 提交到的队列,任务组里不同任务的队列可以不同
block 提交的任务
3、dispatch_group_enter/dispatch_group_leave
func dispatch_group_enter(_ group: dispatch_group_t!)
func dispatch_group_leave(_ group: dispatch_group_t!)
这两个方法显示的将任务组中的任务未执行完毕的任务数目加减1,这种方式用在不使用dispatch_group_async来提交任务,注意:这两个函数要配合使用,有enter要有leave,这样才能保证功能完整实现。也可以用这对函数来让一个闭包关联多个Group
4、dispatch_group_notify 用来监听任务组事件的执行完毕
func dispatch_group_notify(_ group: dispatch_group_t!,
_ queue: dispatch_queue_t!,
_ block: dispatch_block_t!)
参数: group监听的任务组
queue 执行完毕的这个闭包所在的队列
block 执行完毕所响应的任务
5、dispatch_group_wait 设置等待时间,在等待时间结束后,如果还没有执行完任务组,则返回。返回0代表执行成功,非0则执行失败
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()
}
}
三、关于如何使用dispatch_group_enter/dispatch_group_leave如何使用
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()
}
}
这样的话,输出是
[plain] view plain copy
Downloading task1
Downloading task2
Downloading task3
Now viewDidLoad is done
Task1 is done
Task2 is done
Task3 is done
由于三个模拟的下载任务是并行的,所以实际消耗时间3s