ios – 如何判断循环中的块是否都已完成执行?

我有一个循环设置,下载一系列的图像,我以后将使用动画使用UI ImageView的animationImages属性.我想知道我的循环中的所有块是否完成执行,所以我可以开始动画,并且想知道我可以在何时完成完成?谢谢!
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,NSError *error) {
        NSLog(@"Error %@",error);
    }];

}
//When I know all the blocks have finished downloading,I will then to animate the downloaded images.

编辑:问题与错误-999

在提供的答案中执行代码时遇到以下问题:Domain = NSURLErrorDomain Code = -999“无法完成操作(NSURLErrorDomain错误-999.)”

快速搜索reveals,错误-999表示“在上一个请求完成之前提出了另一个请求”…这是肯定的,因为我正在快速连接几个请求.建议的修正建议here对我来说不起作用,因为它只会成功下载一个UIImage(最后一个请求),以前的失败.我想知道有没有解决方法在这里或在AFNetworking我应该考虑?谢谢!

编辑2:基于@ David的解决方案的工作代码

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");

});

解决方法

创建一个调度组,在for循环中输入组,在完成块中离开组.然后可以使用dispatch_group_notify来查明所有块是否完成:
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,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,error);
        dispatch_group_leave(group);
    }];

}

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

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...