Cowboy Tech

WWWDC2016学习笔记-What’s_new_in_GCD

Dispatch Queues execute work items in FIFO order,including two kind of execution

Asynchronous :可以先加入队列,将来再执行

Synchronous:必须执行完才到下一个

let queue = DispatchQueue(label: "com.example.imagetransform")
queue.async {
let smallImage = image.resize(to: rect)
DispatchQueue.main.async {
imageView.image = smallImage
}
}

Getting Work Off Your Main Thread

Create a Dispatch Queue to which you submit work

Dispatch Queues execute work items in FIFO order

Use .async to execute your work on the queue

Dispatch main queue executes all items on the main thread

Simple to chain work between queues

let queue = DispatchQueue(label: "com.example.imagetransform")
queue.async {
let smallImage = image.resize(to: rect)
DispatchQueue.main.async {
imageView.image = smallImage
}
}

Grouping Work Together

Choosing a Quality of Service

Using Quality of Service Classes

Use .async to submit work with a specific QoS class

Dispatch helps resolve priority inversions

Create single-purpose queues with a specific QoS class

queue.async(qos: .background) {
print("Maintenance work")
}
queue.async(qos: .userInitiated) {
print(“Button tapped”)
}