在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中,首选的方式是什么?
当前回答
在>=Swift 3下编译。这个例子包含了我们需要的大部分语法。
QoS——新的服务质量语法
弱自中断保留周期
如果self不可用,什么都不做
Async全局实用程序队列——用于网络查询,不等待结果,它是一个并发队列,块(通常)在启动时不等待。并发队列的例外可能是,当它的任务限制已经达到时,队列临时变成一个串行队列并等待,直到该队列中的某个先前的任务完成。
async主队列——对于触摸UI,块不等待结果,而是等待它在开始时的槽。主队列是一个串行队列。
当然,你需要添加一些错误检查…
DispatchQueue.global(qos: .utility).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
}
}
}
}
其他回答
let newQueue = DispatchQueue(label: "newname")
newQueue.sync {
// your code
}
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 serialQueue = DispatchQueue(label: "my serial queue")
默认值是serial,要获得concurrent,可以使用可选属性参数.concurrent
DispatchQueue.main.async(execute: {
// code
})
Swift 3
DispatchQueue.main.async {
// Write your code here
}