【发布时间】:2017-06-08 14:40:12
【问题描述】:
我有一个 PHAsset 列表,我需要获取它们的关联 URL 并对每个 URL 执行一些操作。处理完所有资产后,我需要执行另一项任务。我尝试使用 __block 来计算处理的资产,但由于竞争条件它不可靠。有没有更好的方法来了解所有资产何时被处理?
PHFetchResult* photosAsset = [PHAsset fetchAssetsInAssetCollection:collection options:fetchOptions2];
__block int count = 0;
for (int i = 0; i < photosAsset.count; ++i) {
[[PHImageManager defaultManager] requestAVAssetForVideo:[photosAsset objectAtIndex:i] options:nil resultHandler:
^(AVAsset *avAsset, AVAudioMix *audioMix, NSDictionary *info) {
NSURL *url = [(AVURLAsset *)avAsset URL];
// then do something with the url here...
++count;
NSLog(@"%d", count);
if (count == photosAsset.count) {
NSLog(@"FINISHED!");
}
}];
}
【问题讨论】:
-
可能是
dispatch_group_enter()、dispatch_group_leave()和dispatch_group_notify()?
标签: ios objective-c asynchronous objective-c-blocks phasset