在
Objective-c中的我们常用的GCD如何在Swift中使用呢?例如:dispatch_async,dispatch_afteretc.
- 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
}
}