Quick brief of what I'm doing: I have two arrays, they both contain 50% of the information for a table view. Why? Because one of the arrays pulls current information from the internet, while the other array has saved user data. I had no idea as to how to get current information from the internet in a non messy way, as I'm an amateur to objective-C let alone networking in Objective-C. Anyway, so the internet Array is pulling information that corresponds to the objects in the saved array (saved in Core Data) using AFNetworking. It's therefore Asynchronous, which I want. Now here comes the problem, or at least what I can garner from the situation.
I'm doing a for loop that effectively counts through the objects in the saved array and passes the unique ID for each object so that the corresponding information from the internet can be downloaded, parsed and added to the internet array. However, since the networking is Asynchronous, the loop is effectively downloading all the information from the internet at once. Therefore the objects are being written to the internet array in order of which downloaded first. Therefore savedArray[0] does not correspond to the object in internetArray[0]. This is a pretty big flaw as you can imagine, as when I'm inserting the values into my tableView, nothing really matches up/makes sense.
I'm really looking for a way to postpone the downloading of the information, until the previous download has been completed and added to the internetArray, how on earth do I do this? Now for the code. Here is where I get the appropriate key:
for ( int i = 0; i < [mySavedObjects count]; i++) {
MySavedObject* mySavedObject = [mySavedObjects objectAtIndex:i];
[self retrieveMissingInformation: myObject.Id];
[self.tableView reloadData];
}
And here is where I actually get the information (simplified for the sake of space):
- (void)retrieveMissingInformation:(NSString *) Id
{
// create URL here.
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"Success");
// Do some safety checks & parse the JSON, returning a searchResult.
[searchResults addObject:searchResult];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON)
// Do some error messages etc.
}];
[queue addOperation:operation]; //where queue is an ivar NSOperationQueue.
}
Finally, in the cellForRowAtIndexpath, I these use both:
MySavedObject *mySavedObject = [mySavedObjects objectAtIndex:indexPath.row];
&
SearchResult *searchResult = [searchResults objectAtIndex:indexPath.row];
To get the values for the cell.
Sorry for the massively large wall of text. I'm not really good enough to explain things efficiently, often getting tongue-tied on the terminology and having to resort to making up my own exmaples. Any help with ordering this mess and I'd be really grateful.
Regards,
Mike.