Will Smith
Will Smith

Reputation: 349

how to wait for response of multiple request for xml parsing?

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 *)parser method, 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

Upvotes: 1

Views: 390

Answers (3)

Kaz
Kaz

Reputation: 1466

i suggest to use AFNetworking for the HTTP requests.

then you can have a separate class which does the XML parsing.

this will allow you to easily receive the response and pass it to the XML class ( you will have to create a new object of the XML parser class to handle requests separately ) and the XML will parse it.

sometimes when you have different responses of different structure you will have to inform the XML class of the current parsing. for example

MyXMLParserClass *pars = [[MyXMLParserClass alloc] init];
pars.ParsingOperation=1;

or

pars.ParsingOperation=2;

then in the XML parser class you can check the operation.

if(self.operationType==1)
{
   // Your code
}
else if(self.operationType==2)
{
  // your code
}

hope this helps.

Upvotes: 0

RubenVot
RubenVot

Reputation: 186

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/

Upvotes: 1

Zayar Zy
Zayar Zy

Reputation: 46

I think u want to do synchronous http request. You should read about this link. And I found this open source library will help u ASIHTTPRequest

Note: ASIHTTPRequest Project owner is no longer support for that project.

Upvotes: 1

Related Questions