在项目开发中有时候需要把一些循环执行的异步操作加入到group中,让彻底循环完之后再进行下一步操作,直接上代码
创建一个组
swift2.3:
let group = dispatch_group_create()
swift3.0:
let group = DispatchGroup()
循环加载数据
swift2.3:
for _ in array.count {
dispatch_group_enter(group)
dispatch_group_leave(group)
}
swift3.0:
for _ in array {
group.enter()
group.leave()
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
全部加载完后通过闭包通知调用者
swift2.3:
dispatch_group_notify(group, dispatch_get_main_queue()) { () -> Void in
}
swift3.0:
group.notify(queue: DispatchQueue.main) {
}