我有一个循环设置,可以下载一系列图像,稍后我将使用UIImageView的UIImageView属性来使用这些图像来动画。我想知道我的循环中的所有块什么时候已经完成执行,这样我就可以开始动画了,我想知道如何才能知道它们什么时候完成呢?谢谢!
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.)"
快速搜索reveals,Error -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");
});发布于 2014-04-23 19:19:27
创建调度组,在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, 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
});发布于 2014-04-23 19:14:29
数一数你完成了多少。最具挑战性的部分是让它的线程安全。我建议为此创建一个原子计数器类。
通用解决方案!
+ (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!
}];发布于 2014-04-23 19:13:14
设置一个属性并将其初始化为循环数- objects.count。在完成块时,将数字降下来。当你达到零,你就完蛋了。
https://stackoverflow.com/questions/23253175
复制相似问题