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

前端之家收集整理的这篇文章主要介绍了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
});
原文链接:https://www.f2er.com/iOS/336770.html

猜你在找的iOS相关文章