I am trying to retrieve some data from server using some sample code as follows. However it's giving me unpredictable results. I am not sure that is because the block is getting deallocated while in side the loop or getting overwritten in memory. Basically the data does not tally with index I expect.
-(void)retrieveSomeStuff {
for (int i = 0 ; i < items.count; i++)
{
[self retrieveDataForIndex:i
completionHandler:^(NSDictionary *data, NSError *error) {
}];
}
}
-(void) retrieveDataForIndex:i completionHandler:(void(^)(NSDictionary *,NSError*) completionHandler {
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
completionHandler(data,connectionError);
}
}
What is the best way handle this kind of scenario ?