learner2010
learner2010

Reputation: 4167

NSURLConnection for multiple views not receiving data asynchronously

In my iPAD application, I have 6 UITableViews. To get data for each of the tableview, I call a Webservice using NSURLConnection and parse the xml I get back from the Webservice and store data into the database.

Since I have 6 UITableView, I send the Webservice request for each of the views at the same time. However, the problem that I am facing is that, for my app to receive data from the Webservice on the -(void) connection:(NSURLConnection *) connection didReceiveData:(NSData *) data for 1 table view keeps depending upon the database operations performed by the parsers of the other tableviews.

For example, the webservice request for tableview's A, B, C, D are all sent at the same time. if I get back the data on the -(void) connection:(NSURLConnection *) connection didReceiveData:(NSData *) data function, until the xml received is parsed and saved to my database, I am not getting the response back for the other tableviews.

I am unable to figure out what I am doing wrong. I know NSURLConnection is asynchronous but the response I am getting does not seem so.

Here is my code -

For sending the Webservice request -

- (void) callMedicationWebService
{
    conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
    if (conn) 
    {
        webData = [[NSMutableData data] retain];
    }
}

-(void) connection:(NSURLConnection *) connection 
didReceiveResponse:(NSURLResponse *) response 
{
    [webData setLength: 0];
}


-(void) connection:(NSURLConnection *) connection 
    didReceiveData:(NSData *) data 
{
    [webData appendData:data];

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"HH:mm:ss"];
    NSString *alertMessage = [formatter stringFromDate:[NSDate date]];
    [formatter release];

    NSLog(@"got data back from WS %@", alertMessage);
}

-(void) connectionDidFinishLoading:(NSURLConnection *) connection 
{
    [connection release];

    // Parse xml
    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:[CommonHelper decodeHTMLCharactorsFromString:webData]];

    TableAHandler *handler = [[TableAHandler alloc] init];
    [handler initTableAHandler];
    [xmlParser setDelegate:handler];
    [xmlParser setShouldResolveExternalEntities:YES];
    [xmlParser setShouldProcessNamespaces:YES];

    BOOL success = [xmlParser parse];
   }

Would someone be able to help me what I am doing wrong?

Upvotes: 0

Views: 430

Answers (1)

Alon Amir
Alon Amir

Reputation: 5025

Asynchronous doesn't necessarily mean that the callback function itself is called in a separate thread.

if you want all the parsing processes to happen at the same time you're gonna have to move the parsing processes to separate threads.

although the better solution would be not to use 5 different URLRequests, and to use only one that returns all the required information.

Upvotes: 1

Related Questions