在Swift 2中,我能够用以下代码创建队列:
let concurrentQueue = dispatch_queue_create("com.swift3.imageQueue", DISPATCH_QUEUE_CONCURRENT)
但是这不能在Swift 3中编译。
在Swift 3中,首选的方式是什么?
在Swift 2中,我能够用以下代码创建队列:
let concurrentQueue = dispatch_queue_create("com.swift3.imageQueue", DISPATCH_QUEUE_CONCURRENT)
但是这不能在Swift 3中编译。
在Swift 3中,首选的方式是什么?
当前回答
现在很简单:
let serialQueue = DispatchQueue(label: "my serial queue")
默认值是serial,要获得concurrent,可以使用可选属性参数.concurrent
其他回答
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版本形成对比。
Swift 3
DispatchQueue.main.async {
// Write your code here
}
在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(execute: {
// write code
})
串行队列:
let serial = DispatchQueue(label: "Queuename")
serial.sync {
//Code Here
}
并发队列:
let concurrent = DispatchQueue(label: "Queuename", attributes: .concurrent)
concurrent.sync {
//Code Here
}
现在很简单:
let serialQueue = DispatchQueue(label: "my serial queue")
默认值是serial,要获得concurrent,可以使用可选属性参数.concurrent