在tutorial from Ray Wenderlich Series,之一,他用dispatch_get_main_queue()
完成塊內dispatch_get_main_queue如下我需要在完成塊
func startFiltrationForRecord(photoDetails: PhotoRecord, indexPath: NSIndexPath){
if let filterOperation = pendingOperations.filtrationsInProgress[indexPath]{
return
}
let filterer = ImageFiltration(photoRecord: photoDetails)
filterer.completionBlock = {
if filterer.cancelled {
return
}
dispatch_async(dispatch_get_main_queue(), {
self.pendingOperations.filtrationsInProgress.removeValueForKey(indexPath)
self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
})
}
pendingOperations.filtrationsInProgress[indexPath] = filterer
pendingOperations.filtrationQueue.addOperation(filterer)
}
即使他簡要地解釋爲什麼需要完成塊,我想知道是否有人能回答下面的問題
在我自己的應用程序中,我有相當多的地方完成塊(重新加載表視圖代碼在他的完成塊中,但是,我沒有一個dispatch_get_main_queue代碼,這是否意味着對於完成塊中的所有UI相關任務,我需要添加dispatch_get_main_queue
?
是的,因爲您的所有UI更新只會在主線程上發生。 –
如果塊在後臺線程中執行,則只需添加它,例如,如果使用Alamofire,則完成塊已在主線程中,因此您不需要它 – Tj3n
相關:[爲什麼要在主隊列上執行UI更改]( http://stackoverflow.com/questions/18467114/why-must-uikit-operations-be-performed-on-the-main-thread)實際上它可能是重複的,但是好吧 – NSNoob