0

我将在我的应用程序中显示一种 UIActivityIndi​​catorView,同时在 for () 循环中解析多个 JSON 对象。我想不出我必须在哪里放置 [UIActivityIndi​​catorView startAnimating] 和 [UIActivityIndi​​catorView stopAnimating] 以显示解析的真正结束(在完整的 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] 的真正位置,因为它在第一个解析结束时停止(第一个 for () 循环):所以,有一种方法可以等待完成for () 循环在整个循环之后调用 [UIActivityIndi​​catorView stopAnimating] (或其他方法)?谢谢!

替代方案 也许解析太快以至于我看不到 UIActivityIndi​​catorView 出现和加载?在这种情况下,我必须在哪里放置一种计时器(或睡眠(无符号整数))等待几秒钟才能消失?

4

3 回答 3

2

您的问题是您在循环中发出的请求是异步的,因此您的循环不会等待它们完成,它只是快速连续触发每个请求,并且一旦执行了加载数组的代码块数据被接收。

您可以在块中使用一个计数器,每次块完成时都会增加一个计数器,然后一旦计数器与您匹配,您[arrayJSON count]就知道何时停止动画。只记得使用__block存储

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

于 2013-01-30T11:27:45.123 回答
1

解决方案是

  1. 简化代码,以便清楚地知道那里正在做什么(事先声明块)

  2. 计算有多少请求正在运行,只有在所有请求都完成后才执行全局操作。

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];
}
于 2013-01-30T11:26:30.533 回答
0

我通常会说,如果您无法确定循环在代码中的结束位置,则表明您的代码很复杂。你的 for 循环在我认为的 else 语句之前结束。要找出答案,您只需单击起始括号,XCode(如果您正在使用的话)将突出显示代码块......

于 2013-01-30T11:13:34.957 回答