使用 __block 的竞争条件

race condition by using __block

我有一个 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!");
             }
        }];
    }

所以这是基于@Larme 的建议的解决方案:

    dispatch_group_t group = dispatch_group_create();

    for (int i = 0; i < photosAsset.count; ++i) {
        dispatch_group_enter(group);
        [[PHImageManager defaultManager] requestAVAssetForVideo:[photosAsset objectAtIndex:i] options:nil resultHandler:
         ^(AVAsset *avAsset, AVAudioMix *audioMix, NSDictionary *info) {
             dispatch_group_leave(group);
         }];
    }

    dispatch_group_notify(group,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^ {
        NSLog(@"FINISHED!");
    });

    dispatch_release(group);