首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何判断循环中的块是否都已完成执行?

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

Stack Overflow用户
提问于 2014-04-23 19:11:00
回答 4查看 3.9K关注 0票数 13

我有一个循环设置,可以下载一系列图像,稍后我将使用UIImageViewUIImageView属性来使用这些图像来动画。我想知道我的循环中的所有块什么时候已经完成执行,这样我就可以开始动画了,我想知道如何才能知道它们什么时候完成呢?谢谢!

代码语言:javascript
复制
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.)"

快速搜索revealsError -999的意思是“在前一个请求完成之前发出了另一个请求”.这当然是这样的,因为我正在快速地提出几个要求。推荐的修补程序建议here对我不起作用,因为它只会成功下载一个UIImage (最后一个请求),而前面的那个失败了。我想知道在这里还是在AFNetworking有我应该考虑的解决办法?谢谢!

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

代码语言:javascript
复制
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");

});
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-04-23 19:19:27

创建调度组,在for循环中输入组,在完成块中离开组。然后可以使用dispatch_group_notify查找所有块何时完成:

代码语言:javascript
复制
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
});
票数 34
EN

Stack Overflow用户

发布于 2014-04-23 19:14:29

数一数你完成了多少。最具挑战性的部分是让它的线程安全。我建议为此创建一个原子计数器类。

通用解决方案!

代码语言:javascript
复制
+ (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();
    }
}

代码语言:javascript
复制
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!
}];
票数 2
EN

Stack Overflow用户

发布于 2014-04-23 19:13:14

设置一个属性并将其初始化为循环数- objects.count。在完成块时,将数字降下来。当你达到零,你就完蛋了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23253175

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档