我有这段代码,我想在继续 for 循环之前急切地使用AFJSONRequestOperation
它,但不知何故它不起作用。下面的代码在我的 for 循环中。即使请求尚未完成,如何防止我的循环继续进行?这将帮助我按照我想要的顺序将从成功请求中获得的所有值放入数组中。
这是代码:
NSURL *url = [NSURL URLWithString:[nowShowingTitleListArray objectAtIndex:i]];
NSURLRequest *theRequest = [NSURLRequest requestWithURL:url];
[AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:theRequest
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
{
// Code when success
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON)
{
NSLog(@"Request Failed with Error: %@", [error localizedDescription]);
}];
[operation start];
目前,我这样做是为了确保将它们按顺序提取并添加到我的数组中:
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[nowShowingTitleListArray objectAtIndex:i]]];
NSError *error;
NSDictionary *JSONDict = [NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:&error];
if (!JSONDict)
{
NSLog(@"Request Failed with Error: %@", [error localizedDescription]);
}
else
{
// Rest of code
}
当网络连接突然断开时,这也将帮助我最大限度地减少我在请求中遇到的一些崩溃问题。