在objective-c中检测for()循环的实际结束 [英] Detect real end of the for () loop in objective-c

查看:124
本文介绍了在objective-c中检测for()循环的实际结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

   - (void)parseMethod {

//其他的东西

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]];

//这里完成PARESING AFJSONREQUESTOPERATION
//如果我把它放在这里[UIActivityIndi​​catorView stopAnimating]它显示在第一个结尾为()LOOP

[table reloadData ]。
[table scrollRectToVisible:CGRectMake(0,0,1,1)animated:YES]
}
失败:^(NSURLRequest * request,NSHTTPURLResponse * response,NSError * error,id JSON) {
NSLog(@%@,[error userInfo]);
}];
[操作开始];
//这里开始参与AFJSONREQUESTOPERATION
[UIActivityIndi​​catorView startAnimating]
}

//这里完成for()LOOP ??
//如果我放在这里[UIActivityIndi​​catorView stopAnimating]它在第一次结束时再次显示为()LOOP

}

//这里完成parseMethod? ?
//如果我放在这里[UIActivityIndi​​catorView stopAnimating]它在第一次结束时再次显示为()LOOP

}
$ b $你可以看到,我找不到放置[UIActivityIndi​​catorView stopAnimating]的真正位置,因为无处不在它停止在FIRST解析的结尾(第一个for )循环):所以,有一种方法来等待完整的()循环调用[UIActivityIndi​​catorView stopAnimating](或另一种方法)之后的整个循环?

替代
也许解析速度太快,我看不到UIActivityIndi​​catorView出现和加载?在这种情况下,我必须把某种计时器(或睡眠(无符号整数))等待几秒钟,然后消失?

解决方案<解决方案是
$ b


  1. 简化代码,以便明白正在做什么(预先声明块)

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




  3.   typedef void(^ AFJSONSuccessBlock)(NSURLRequest * request,NSHTTPURLResponse * response ,id JSON); 
    typedef void(^ AFJSONFailureBlock)(NSURLRequest * request,NSHTTPURLResponse * response,NSError * error,id JSON);



      [UIActivityIndi​​catorView startAnimating]; 

    __block int numRequests = [arrayJSON count];

    AFJSONSuccessBlock onSuccess = ^(NSURLRequest * request,NSHTTPURLResponse * response,id JSON){
    [...]

    numRequests--;

    if(numRequests == 0){
    [UIActivityIndi​​catorView stopAnimating];
    }
    };

    AFJSONFailureBlock onFailure = ^(NSURLRequest * request,NSHTTPURLResponse * response,NSError * error,id JSON){
    [...]

    numRequests--;

    if(numRequests == 0){
    [UIActivityIndi​​catorView stopAnimating];
    }
    };

    (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 = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
    success:onSuccess
    failure:onFailure];
    [操作开始];
    }


    I'm going to show in my app a sort of UIActivityIndicatorView while parsing several JSON objects, inside a for () loop. I can't figure WHERE I must place the [UIActivityIndicatorView startAnimating] and [UIActivityIndicatorView stopAnimating] to show the real END of parsing (at the end of the complete for () loop). This is the simplified code:

    - (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
    
        }
    

    As u can see, I can't find the true place to put [UIActivityIndicatorView stopAnimating], 'cos everywhere it stop at the end of the FIRST parsing (first for () loop): so, there is a way to WAIT the complete for () loop to call [UIActivityIndicatorView stopAnimating] (or another method) AFTER the ENTIRE cycle? Thanks!

    ALTERNATIVE Maybe the parsing is so fast that I can't see the UIActivityIndicatorView appearing and loading? In this case, WHERE I must put a sort of timer (or the sleep(unsigned int)) to wait a few seconds before it disappear?

    解决方案

    Solution is to

    1. Simplify the code so that is obvious what is being done there (declaring the blocks beforehand)

    2. Count how many requests are running and perform the global actions only when all the requests are completed.

    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];
    }
    

    这篇关于在objective-c中检测for()循环的实际结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆