在
Objective-c
中的我们常用的GCD
如何在Swift
中使用呢?例如:dispatch_async
,dispatch_after
etc.
- Global
DispatchQueue.global(qos: .userInitiated).async {
}
为何用
.userInitiated
代替DISPATCH_QUEUE_PRIORITY
请参考苹果文档
- Main
DispatchQueue.main.async {
}
- After
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { // in half a second...
print("Are we there yet?")
}
- Custom
let queue = DispatchQueue(label: "com.zhuo.my-serial-queue",
attributes: [.serial, .qosUtility])
func doStuff() {
queue.async {
print("Hello")
}
}
下载图片
DispatchQueue.global(qos: .userInitiated).async {
let image = self.loadAnImage()
// To the main thread to update the UI
DispatchQueue.main.async {
self.imageView.image = image
}
}