在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