GCD & Swift3

作者: 孟熠冉的简书 | 来源:发表于2017-06-02 15:30 被阅读5次

    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

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