How to tell if blocks in loop all have completed executing?

ios, objective-c, objective-c-blocks, parse-platform

Solution

Create a dispatch group, in the for loop enter the group, in the completion block leave the group. Then you can use dispatch_group_notify to find out when all blocks have completed:

dispatch_group_t    group = dispatch_group_create();

for (PFObject *pictureObject in objects){

    PFFile *imageFile = [pictureObject objectForKey:@"image"];
    NSURL *imageFileURL = [[NSURL alloc] initWithString:imageFile.url];
    NSURLRequest *imageRequest = [NSURLRequest requestWithURL:imageFileURL];

    dispatch_group_enter(group);
    [tokenImageView setImageWithURLRequest:imageRequest placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
        [self.downloadedUIImages addObject:image]; //This is a mutableArray that will later be set to an UIImageView's animnationImages
        dispatch_group_leave(group);
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
        NSLog(@"Error %@", error);
        dispatch_group_leave(group);
    }];

}

dispatch_group_notify(group, dispatch_get_main_queue(), ^{
    // do your completion stuff here
});

Problem

I have a loop set up that downloads a series a images which I will later use for to animate using the `animationImages` property of `UIImageView`. I would like to know when all the blocks inside my loops have finished executing so I could begin the animation, and was wondering how I may be able to tell when they are finished completing? Thanks! ``` for (PFObject *pictureObject in objects){ PFFile *imageFile = [pictureObject objectForKey:@"image"]; NSURL *imageFileURL = [[NSURL alloc] initWithString:imageFile.url]; NSURLRequest *imageRequest = [NSURLRequest requestWithURL:imageFileURL]; [tokenImageView setImageWithURLRequest:imageRequest placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) { [self.downloadedUIImages addObject:image]; //This is a mutableArray that will later be set to an UIImageView's animnationImages } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) { NSLog(@"Error %@", error); }]; } //When I know all the blocks have finished downloading, I will then to animate the downloaded images. ``` Edit: having issue with `Error -999` I am encountering the following issue when executing the code in the provided answer: `Domain=NSURLErrorDomain Code=-999 "The operation couldn’t be completed. (NSURLErrorDomain error -999.)"` A quick search reveals that `Error -999` means "another request is made before the previous request is completed" ... which is certainly the case here since I am making several requests in quick succession. The recommended fix suggested here didn't work for me as it will only successfully download one UIImage (the last one requested) , with the previous ones failing. I was wondering if there is workaround here or in AFNetworking that I ought to consider? Thanks! Edit 2: working code based on @David's solution ``` for (PFObject *pictureObject in objects){ PFFile *imageFile = [pictureObject objectForKey:@"image"]; NSURL *imageFileURL = [[NSURL alloc] initWithString:imageFile.url]; NSURLRequest *imageRequest = [NSURLRequest requestWithURL:imageFileURL]; AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:imageRequest]; requestOperation.responseSerializer = [AFImageResponseSerializer serializer]; dispatch_group_enter(group); [requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"Response: %@", responseObject); UIImage *retrivedImage = (UIImage *)responseObject; [self.downloadedUIImages addObject:retrivedImage]; dispatch_group_leave(group); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Image error: %@", error); dispatch_group_leave(group); }]; [requestOperation start]; counter ++; } dispatch_group_notify(group, dispatch_get_main_queue(), ^{ NSLog(@"Horray everything has completed"); NSLog(@"What is here %@", self.downloadedUIImages); NSLog(@"Done"); }); ```

Original source

Related problems