I have 5 html request to get data from web service. I am requesting in for loop.
-(void)loadPreviewsData
{
NSMutableURLRequest *request;
for (int i=1; i<=5; i++)
{
if(i==1)
{
//request 1
}
if(i==2)
{
//request 2
}
//3 to 5
request.HTTPMethod = @"POST";
NSURLConnection *myConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
if(myConnection)
{
HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
HUD.labelText=@"Please wait.";
myData = [[NSMutableData alloc] initWithLength:0];
}
else
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Message" message:@"Connection could not be established!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
}// end for loop
}//end loadPreview
all response has a lot of data and i am saving response data into array, but it is not waiting for first response and making another request.
finally for loop will finish and next view will load but array is not filled because parsing is not done yet because copy of data into array is not completed so there is an error.i tried to
sleep(5);in-(void)parserDidEndDocument:(NSXMLParser *)parsermethod, but it is not valid, because response time is depends on network traffic. i can’t understand what to do please help.I tried Following answer also but can’t understand Wait for code to finish execution
Probably you are using asynchronous methods with same delegate.
This means you make the 5 requests at the same time.
You could make a queue in Grand Central Dispatch:
http://www.fieryrobot.com/blog/2010/06/27/a-simple-job-queue-with-grand-central-dispatch/