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

我有一个循环设置,下载一系列的图像,我将稍后使用animation使用UIImageViewanimationImages属性。 我想知道什么时候我的循环里面的所有块已经完成执行,所以我可以开始animation,并想知道如何能够告诉他们什么时候完成? 谢谢!

 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. 

编辑:有Error -999问题

我在提供的答案中执行代码时遇到以下问题: Domain=NSURLErrorDomain Code=-999 "The operation couldn't be completed. (NSURLErrorDomain error -999.)"

快速search显示 Error -999意味着“ 另一个请求是在前一个请求完成之前进行的 ……”这是在这里确实是这种情况,因为我正在快速连续地提出几个请求。 这里build议的修复方法对我来说不起作用,因为它只会成功下载一个UIImage(最后一个请求),而之前的UIImage会失败。 我想知道在这里或AFNetworking是否有解决方法,我应该考虑? 谢谢!

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

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

创build一个调度组,在for循环中input组,在完成块中离开组。 然后你可以使用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, 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 }); 

数你已经完成了多less。 具有挑战性的部分是使线程安全。 我build议创build一个primefaces计数器类。

通用解决scheme!

 + (void)runBlocksInParallel:(NSArray *)blocks completion:(CompletionBlock)completion { AtomicCounter *completionCounter = [[AtomicCounter alloc] initWithValue:blocks.count]; for (AsyncBlock block in blocks) { block(^{ if ([completionCounter decrementAndGet] == 0) { if (completion) completion(); } }); } if (blocks.count == 0) { if (completion) completion(); } } 

 NSMutableArray *asyncBlocks = [NSMutableArray array]; for (PFObject *pictureObject in objects){ [asyncBlocks addObject:^(CompletionBlock completion) { 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); } completion:completion]; }]; } [BlockRunner runBlocksInParallel:[asyncBlocks copy] completion:^{ //Do your final completion here! }]; 

设置一个属性并将其初始化为周期数 – objects.count 。 在完成该块,降低数量。 当你达到零时,你就完成了。

 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 if([[objects lastObject] isEqual:pictureObject]) { [self animateImages]; } } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) { NSLog(@"Error %@", error); if([[objects lastObject] isEqual:pictureObject]) { [self animateImages]; } }]; } - (void)animateImages { //do animation here. }