在objective-c中检测for()循环的实际结束 [英] Detect real end of the for () loop in objective-c
问题描述
- (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
//如果我把它放在这里[UIActivityIndicatorView 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
[UIActivityIndicatorView startAnimating]
}
//这里完成for()LOOP ??
//如果我放在这里[UIActivityIndicatorView stopAnimating]它在第一次结束时再次显示为()LOOP
}
//这里完成parseMethod? ?
//如果我放在这里[UIActivityIndicatorView stopAnimating]它在第一次结束时再次显示为()LOOP
}
$ b $你可以看到,我找不到放置[UIActivityIndicatorView stopAnimating]的真正位置,因为无处不在它停止在FIRST解析的结尾(第一个for )循环):所以,有一种方法来等待完整的()循环调用[UIActivityIndicatorView stopAnimating](或另一种方法)之后的整个循环? 替代
也许解析速度太快,我看不到UIActivityIndicatorView出现和加载?在这种情况下,我必须把某种计时器(或睡眠(无符号整数))等待几秒钟,然后消失?
$ b
-
简化代码,以便明白正在做什么(预先声明块)
-
计算有多少个请求正在运行,只有当所有请求都完成时才执行全局动作。
Simplify the code so that is obvious what is being done there (declaring the blocks beforehand)
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];
}
};
(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
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屋!