Concurrent queue并行處理任務(wù)
每個(gè)app默認(rèn)提供了4個(gè)可以并行執(zhí)行任務(wù)的隊(duì)列(concurrent queue)
它分成4個(gè)不同的優(yōu)先級(jí),由高到低分別是:
DISPATCH_QUEUE_PRIORITY_HIGH
DISPATCH_QUEUE_PRIORITY_DEFAULT
DISPATCH_QUEUE_PRIORITY_LOW
DISPATCH_QUEUE_PRIORITY_BACKGROUND
2.1
@IBAction func downloadImages(sender: UIButton) {
// Add concurrent queue
let currQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
// 自定義并行隊(duì)列
//let currQueue = dispatch_queue_create("customCurrQueue", DISPATCH_QUEUE_CONCURRENT)
for i in 0..<4 {
dispatch_async(currQueue) { () -> Void in
let imageViewUrls = Downloader.downloadImageWithURL(self.imageUrls[i])
// UI需要在主線程下更新
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.imageViewArr[i].image = imageViewUrls
print(i)
})
}
}