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
}
これにより、ネットワーク接続が突然切断されたときにリクエストで発生するクラッシュの問題を最小限に抑えることもできます。