美文网首页
GCD in Swift

GCD in Swift

作者: Show_Perry | 来源:发表于2017-07-04 15:40 被阅读26次

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
    }
}

相关文章

网友评论

      本文标题:GCD in Swift

      本文链接:https://www.haomeiwen.com/subject/eghxhxtx.html