检测objective-c中for()循环的实际结束

时间:2013-01-30 11:09:09

标签: objective-c parsing loops for-loop

我将在我的应用程序中显示一种UIActivityIndi​​catorView,同时在for()循环中解析几个JSON对象。我无法想象我必须放置[UIActivityIndi​​catorView startAnimating]和[UIActivityIndi​​catorView stopAnimating]以显示解析的真实END(在完成for()循环的末尾)。这是简化的代码:

- (void)parseMethod {

// other stuff

        for (int i=0; i < [arrayJSON count]; i++) {

            NSURL *url = [NSURL URLWithString:
                          [NSString stringWithFormat:
                           @"http://WWW.REMOTESERVERWITHJSONSOURCE.NET/%@",[arrayJSON objectAtIndex:i]]];

            NSURLRequest *request = [NSURLRequest requestWithURL:url];
            AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {

                NSMutableDictionary *arrayMain = [JSON objectForKey:@"main"];
                NSMutableDictionary *arrayMain2 = [JSON objectForKey:@"main2"];
                [arrayA1 addObject:[arrayMain valueForKey:@"A1"]];
                [arrayA2 addObject:[arrayWind valueForKey:@"A2"]];

                // HERE FINISH PARSING AFJSONREQUESTOPERATION
                // IF I PUT HERE [UIActivityIndicatorView stopAnimating] IT SHOWS AT THE END OF FIRST for () LOOP

                [table reloadData];
                [table scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES]
            }
                                                                            failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
                                                                            NSLog(@"%@", [error userInfo]);
                                                                        }];
            [operation start];
            // HERE START PARSING AFJSONREQUESTOPERATION
            [UIActivityIndicatorView startAnimating] 
        }

    // HERE FINISH THE for () LOOP ??
    // IF I PUT HERE [UIActivityIndicatorView stopAnimating] IT SHOWS AGAIN AT THE END OF FIRST for () LOOP

} 

    // HERE FINISH THE parseMethod ??
    // IF I PUT HERE [UIActivityIndicatorView stopAnimating] IT SHOWS AGAIN AT THE END OF FIRST for () LOOP

    }

正如你所看到的,我无法找到放置[UIActivityIndi​​catorView stopAnimating]的真实位置,'cos它在FIRST解析结束时停止的任何地方(首先用于()循环):所以,有一种方法可以等待整个for()循环在整个循环后调用[UIActivityIndi​​catorView stopAnimating](或其他方法)?谢谢!

ALTERNATIVE 也许解析速度太快,我看不到UIActivityIndi​​catorView出现和加载?在这种情况下,在哪里我必须把一种计时器(或睡眠(unsigned int))等待几秒才消失?

3 个答案:

答案 0 :(得分:2)

你的问题是你在循环中发出的请求是异步的,所以你的循环不会让它们完成,它只是快速连续触发每个循环,你加载数组的代码块是收到数据后执行。

你可以在你的块中使用一个计数器,每当一个块完成时它会递增,然后一旦计数器与你知道何时停止动画的[arrayJSON count]匹配。请记住使用__block存储

http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/Blocks/Articles/bxVariables.html

答案 1 :(得分:1)

解决方案是

  1. 简化代码,以便明白在那里做的事情(预先声明块)

  2. 计算正在运行的请求数,并仅在完成所有请求后执行全局操作。

  3. typedef void (^AFJSONSuccessBlock) (NSURLRequest *request, NSHTTPURLResponse *response, id JSON);
    typedef void (^AFJSONFailureBlock) (NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON);
    
    [UIActivityIndicatorView startAnimating];
    
    __block int numRequests = [arrayJSON count];
    
    AFJSONSuccessBlock onSuccess = ^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {   
        [...]
    
        numRequests--;
    
        if (numRequests == 0) {
            [UIActivityIndicatorView stopAnimating];
        }
    };
    
    AFJSONFailureBlock onFailure = ^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
        [...]
    
        numRequests--;
    
        if (numRequests == 0) {
            [UIActivityIndicatorView stopAnimating];
        }
    };
    
    for (NSString* jsonPath in arrayJSON) {
        NSString* absolutePath = [NSString stringWithFormat:@"http://WWW.REMOTESERVERWITHJSONSOURCE.NET/%@", jsonPath];
        NSURL *url = [NSURL URLWithString:absolutePath];
    
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        AFJSONRequestOperation *operation;
        operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
                                                                    success:onSuccess
                                                                    failure:onFailure];
        [operation start];
    }
    

答案 2 :(得分:0)

我通常会说,如果您无法确定代码中循环的结束位置,则表明您的代码会被复杂化。你的for循环在我认为的else语句之前结束。要找出你可以简单地点击起始括号和XCode(如果那是你正在使用的)将突出显示代码块......