首页 文章

如何在Swift 3中创建调度队列

提问于
浏览
370

在Swift 2中,我能够使用以下代码创建队列:

let concurrentQueue = dispatch_queue_create("com.swift3.imageQueue", DISPATCH_QUEUE_CONCURRENT)

但这不能在Swift 3中编译 .

在Swift 3中写这个的首选方法是什么?

14 回答

  • 6
    DispatchQueue.main.async(execute: {
    
    // write code
    
    })
    

    串行队列:

    let serial = DispatchQueue(label: "Queuename")
    
    serial.sync { 
    
     //Code Here
    
    }
    

    并发队列:

    let concurrent = DispatchQueue(label: "Queuename", attributes: .concurrent)
    
    concurrent.sync {
    
     //Code Here
    }
    
  • 2

    对于Swift 3

    DispatchQueue.main.async {
            // Write your code here
        }
    
  • 2
    let newQueue = DispatchQueue(label: "newname")
     newQueue.sync { 
    
     // your code
    
     }
    
  • 2

    它现在简单地说:

    let serialQueue = DispatchQueue(label: "my serial queue")
    

    默认为serial,要获得并发,请使用可选属性参数.concurrent

  • 0

    您可以在swift 3.0中使用此代码创建调度队列

    DispatchQueue.main.async
     {
       /*Write your code here*/
     }
    
       /* or */
    
    let delayTime = DispatchTime.now() + Double(Int64(0.5 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)                   
    DispatchQueue.main.asyncAfter(deadline: delayTime)
    {
      /*Write your code here*/
    }
    
  • 1
    DispatchQueue.main.async(execute: {
       // code
    })
    
  • 2

    我这样做了,如果您想刷新UI以显示新数据而没有用户注意到UITableView或UIPickerView,这一点尤其重要 .

    DispatchQueue.main.async
     {
       /*Write your thread code here*/
     }
    
  • 53
    let concurrentQueue = dispatch_queue_create("com.swift3.imageQueue", DISPATCH_QUEUE_CONCURRENT) //Swift 2 version
    
       let concurrentQueue = DispatchQueue(label:"com.swift3.imageQueue", attributes: .concurrent) //Swift 3 version
    

    我在Xcode 8,Swift 3中重新编写了代码,并且更改标记为与Swift 2版本形成对比 .

  • -3
    DispatchQueue.main.async {
              self.collectionView?.reloadData() // Depends if you were populating a collection view or table view
        }
    
    
    OperationQueue.main.addOperation {
        self.lblGenre.text = self.movGenre
    }
    

    //如果需要在viewcontroller上填充对象(labels,imageview,textview),请使用Operation Queue

  • -3

    斯威夫特3

    你想在swift代码中调用一些闭包然后你想在故事板中更改ya任何类型的更改属于视图你的应用程序将崩溃

    但是你想使用调度方法你的应用程序不会崩溃

    异步方法

    DispatchQueue.main.async 
    {
     //Write code here                                   
    
    }
    

    同步方法

    DispatchQueue.main.sync 
    {
         //Write code here                                  
    
    }
    
  • 1049

    创建并发队列

    let concurrentQueue = DispatchQueue(label: "queuename", attributes: .concurrent)
    concurrentQueue.sync {
    
    }
    

    创建一个串行队列

    let serialQueue = DispatchQueue(label: "queuename")
    serialQueue.sync { 
    
    }
    

    异步获取主队列

    DispatchQueue.main.async {
    
    }
    

    同步获取主队列

    DispatchQueue.main.sync {
    
    }
    

    获得一个后台线程

    DispatchQueue.global(qos: .background).async {
    
    }
    

    Xcode 8.2 beta 2:

    获得一个后台线程

    DispatchQueue.global(qos: .default).async {
    
    }
    
    DispatchQueue.global().async {
        // qos' default value is ´DispatchQoS.QoSClass.default`
    }
    

    如果您想了解如何使用这些队列 . 请参阅answer

  • 7

    Swift 3 下编译 . 此示例包含我们需要的大部分语法 .

    QoS - 新的服务质量语法

    weak self - 中断保留周期

    如果自己不可用,什么也不做

    async global background queue - 用于网络查询

    async main queue - 用于触摸UI .

    当然,您需要为此添加一些错误检查...

    DispatchQueue.global(qos: .background).async { [weak self] () -> Void in
    
        guard let strongSelf = self else { return }
    
        strongSelf.flickrPhoto.loadLargeImage { loadedFlickrPhoto, error in
    
            if error != nil {
                print("error:\(error)")
            } else {
                DispatchQueue.main.async { () -> Void in
                    activityIndicator.removeFromSuperview()
                    strongSelf.imageView.image = strongSelf.flickrPhoto.largeImage
                }
            }
        }
    }
    
  • -3

    编译于XCode 8,Swift 3 https://github.com/rpthomas/Jedisware

    @IBAction func tap(_ sender: AnyObject) {
    
        let thisEmail = "emailaddress.com"
        let thisPassword = "myPassword" 
    
        DispatchQueue.global(qos: .background).async {
    
            // Validate user input
    
            let result = self.validate(thisEmail, password: thisPassword)
    
            // Go back to the main thread to update the UI
            DispatchQueue.main.async {
                if !result
                {
                    self.displayFailureAlert()
                }
    
            }
        }
    
    }
    
  • 29

    由于上面已经回答了OP问题,我只想添加一些速度考虑因素:

    在DispatchQueue.global中为异步函数指定的优先级等级会有很大的不同 .

    我不建议使用.background线程优先级运行任务,尤其是在iPhone X上,其中任务似乎分配在低功耗内核上 .

    以下是来自计算密集型函数的一些实际数据,这些函数从XML文件读取(带缓冲)并执行数据插值:

    设备名称/ .background / .utility / .default / .userInitiated / .userInteractive

    • iPhone X:18.7s / 6.3s / 1.8s / 1.8s / 1.8s

    • iPhone 7:4.6s / 3.1s / 3.0s / 2.8s / 2.6s

    • iPhone 5s:7.3s / 6.1s / 4.0s / 4.0s / 3.8s

    请注意,所有设备的数据集都不相同 . 它是iPhone X上最大的,也是iPhone 5s上最小的 .

相关问题