GCD & Swift3

1、最常用的在一个异步队列中获取数据,然后返回主线程更新UI

DispatchQueue.global().async {
    DispatchQueue.main.async {
    self.label?.text ="finished"
    }
}

2、优先级的改进

DISPATCH_QUEUE_PRIORITY_HIGH:  .userInitiated

DISPATCH_QUEUE_PRIORITY_DEFAULT:  .default

DISPATCH_QUEUE_PRIORITY_LOW: .utility

DISPATCH_QUEUE_PRIORITY_BACKGROUND: .background

eg:

  DispatchQueue.global(qos: .userInitiated).async {

  }

3、DispatchWorkItem 的使用

et queue = DispatchQueue(label:"swift.queue")
let workItem = DispatchWorkItem(qos: .userInitiated, flags: .assignCurrentContext) {

}
queue.async(execute: workItem)

相当于:使用Global Queue
DispatchQueue.global(qos:.userInitiated).async {

}

4、对dispatch_time_t的改进

let delay = DispatchTime.now() + .seconds(60)

DispatchQueue.main.after(when: delay) {
    // Do something
}

你可能感兴趣的:(GCD & Swift3)