Reputation: 4079
I have this code that I want to eager load using AFJSONRequestOperation
before continuing with the for loop but somehow it doesn't work. The code below is inside my for loop. How can I prevent my loop from proceeding even if the request is not yet complete? This will help me place all the values got from a success request inside an array in the order I wanted it.
This is the code:
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];
Currently, I'm doing this to ensure that they'll be fetched and added to my array in order:
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
}
This will also help me minimize some of the crash issues I'm having with the request when the network connection suddenly drops out.
Upvotes: 0
Views: 120
Reputation: 509
Why you are you fetching your data from the same URL twice? Once using AFJSONRequestOperation and then dataWithContentsOfURL: Are you doing this in the same for loop?
You already have your JSON object in success block
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
{
// Code when success
// write success code here
}
you can call AFJSONRequestOperation in loop for multiple network requests.
Upvotes: 0