编程逻辑如下。 在后台主题中加载媒体中的媒体, 每当medias计数%30 == 0时,它将在主线程中重新加载collectionView。
这是我的代码
backgroundThread函数
func backgroundThread(delay: Double = 0.0, background: (() -> Void)? = nil, completion: (() -> Void)? = nil) {
dispatch_async(dispatch_get_global_queue(Int(QOS_CLASS_USER_INITIATED.value), 0)) {
if(background != nil){ background!(); }
let popTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delay * Double(NSEC_PER_SEC)))
dispatch_after(popTime, dispatch_get_main_queue()) {
if(completion != nil){ completion!(); }
}
}
}
loadMedia函数
func loadMedias() {
Log.shareInstance.p("PTVideo - loadMedias begin", caller: self)
assetLibrary.enumerateGroupsWithTypes(ALAssetsGroupSavedPhotos, usingBlock: { (grp: ALAssetsGroup!, groupBool: UnsafeMutablePointer<ObjCBool>) -> Void in
// do something here
if ALAssetsFilter.allVideos() != nil && grp != nil {
if grp != nil {
grp.enumerateAssetsUsingBlock({ (asset, index, assetBool) -> Void in
if asset != nil {
self.medias.append(asset!)
if(count(self.medias) % 30 == 0){
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.mediaCollection.reloadData()
})
}
}
})
}
}
}) { (error) -> Void in
// Handle error
println("error in fetching assets")
}
}
以这种方式在viewDidLoad函数中调用它们
self.backgroundThread(delay: 0, background: { () -> Void in
self.loadMedias()
}, completion: nil)