0

I have an app which combines multiple videos, the initial list of PHAssets are displayed and selected to form an array of PHAssets. Now on the screen which creates the video I need to loop through and fetch the AVAsset from the PHAsset.

The issue I am trying to understand is how to track the progress and determine the end of all the asynchronous fetches. When the loop is complete I can move onto actually combining all the videos.

 for (PHAsset * object in self.arraySelectedAssets) {

        [[PHImageManager defaultManager] requestAVAssetForVideo:object options:nil resultHandler:^(AVAsset *avAsset, AVAudioMix *audioMix, NSDictionary *info) {

            NSLog(@"Fetched");

            //here asset in nil! IOS 10 only, IOS 11 works fine
            AVURLAsset * assetUrl = (AVURLAsset*)avAsset;


        }];

    }
ORStudios
  • 3,157
  • 9
  • 41
  • 69
  • 2
    Take a look at [dispatch_group](https://developer.apple.com/documentation/dispatch/1452975-dispatch_group_create?language=objc) – vadian Aug 28 '18 at 08:43

1 Answers1

1

You can have store the length of self.arraySelectedAssets and have a counter var declared outside the loop.

Then, in each callback you increment the counter variable and also add an if to check if the counter is equal to the length.

In that case you know that you have all the assets.

int total = [self.arraySelectedAssets count];
int count = 0;

for (PHAsset * object in self.arraySelectedAssets) {

    [[PHImageManager defaultManager] requestAVAssetForVideo:object options:nil resultHandler:^(AVAsset *avAsset, AVAudioMix *audioMix, NSDictionary *info) {

        NSLog(@"Fetched");

        //here asset in nil! IOS 10 only, IOS 11 works fine
        AVURLAsset * assetUrl = (AVURLAsset*)avAsset;

        count ++;

        if (count == total) {
           //do your stuff
        }

    }];

}
Radu Diță
  • 13,476
  • 2
  • 30
  • 34