如果想在dispatch_queue中所有的任務(wù)執(zhí)行完成后在做某種操作,在串行隊(duì)列中烹骨,可以把該操作放到最后一個(gè)任務(wù)執(zhí)行完成后繼續(xù)熙掺,但是在并行隊(duì)列中怎么做呢佑钾。這就有dispatch_group 成組操作伞辛。比如
? ? ? ? 緩存圖片是異步的,我要讓圖片都下載完夯缺,再讓程序繼續(xù)往下走蚤氏!
//緩存圖片
private func cacheImages(viewModels : [StatusViewModel]) {
// 0.創(chuàng)建group
? ? ? ? ? ? ? letgroup =dispatch_group_create()
// 1.緩存圖片
forviewmodelinviewModels {
? ? ? ? ? ? forpicURLinviewmodel.picURLs{
? ? ? ? ? ? ? ? ?dispatch_group_enter(group)
? ? ? ? ? ? ? ? ?SDWebImageManager.sharedManager().downloadImageWithURL(picURL, options: [], ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?progress:nil, completed: { (_,_,_,_,_) ->Void in
? ? ? ? ? ? ? ? ? ? ? ? ? dispatch_group_leave(group)
? ? ? ? ? ? ? ? ?})
? ? ? ? ? ? ?}
}
// 2.刷新表格
dispatch_group_notify(group,dispatch_get_main_queue()) { () ->Void in
//刷新表格
self.tableView.reloadData()
//停止刷新
self.tableView.mj_header.endRefreshing()
self.tableView.mj_footer.endRefreshing()
//顯示提示的Label
self.showTipLabel(viewModels.count)
}
}