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

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

但是这不能在Swift 3中编译。

在Swift 3中,首选的方式是什么?


当前回答

 let newQueue = DispatchQueue(label: "newname")
 newQueue.sync { 

 // your code

 }

其他回答

   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版本形成对比。

 let newQueue = DispatchQueue(label: "newname")
 newQueue.sync { 

 // your code

 }

在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()
            }

        }
    }

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

//使用操作队列如果你需要填充对象(标签,imageview, textview)在你的视图控制器

创建并发队列

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

如果你想了解如何使用这些队列。请看这个答案