Question

I have send asynchronous request to a website using the following code:

NSMutableURLRequest *requestSiteToSendData = [NSMutableURLRequest requestWithURL:[[NSURL alloc]initWithString:@"www.example.com"] cachePolicy:NSURLRequestReloadIgnoringCacheData  timeoutInterval:30];
NSURLConnection *connectionToSiteToSendData = [[NSURLConnection alloc]initWithRequest:requestSiteToSendData delegate:self];

Then I used the following method defined inside NSURLConnectionDelegate to get and parse the data after the data fetching is completed.

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    //parse 'data'
    NSString *parsedData = [self parseDataWithData:data];
}

And then in the method in which I send the asynchronous request, I return parsedData. But the returning should only happen after the data fetching is completed and hence parsing is done. I know the question arises if that is what I need then why I am not using synchronous request. It is because I don't want my other methods to hang up when the loading is going on in background.

Was it helpful?

Solution

Quick answer : if it's asynchronous, you don't want to wait the asynchronous method. One of the bests option would be :

  • The object calling wanting the data should set itself as the object that runs the asynchronous method, and in didReceiveData, you call a method such as updateData:(NSString *)parsedData, which handles the newly received data

  • The object calling the method should use KVO to observe any change on a property of the object that runs the asynchronous method.

Tell me if you need more informations.

OTHER TIPS

Asynchronous requests run on separate thread, So we don't need to worry about handling view lockup. If you want send a synchronous request then you have to use GCD to achieve the same. And various other details like, how much data is send/received etc. will not be available in synchronous request.

Synchronous request are helpful if your code next state is dependent on data received in response of the request.

As far as i understand you want that to return data after web call is complete. so i would suggest that create any method for webcall that returns NSData and do something like this:

NSHTTPURLResponse* urlResponse = nil;
NSError *error = [[NSError alloc] init];  
NSData *responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&urlResponse error:&error];  

if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) {
       // return responseData from here.
}
else {
        NSLog(@"%d",[urlResponse statusCode]);
        NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
        NSLog(@"%@",result);

}

and you don't want to hung up your View. so call this method in background thread. like this:

[self performSelectorInBackground:@selector(WebCallMethod) withObject:nil];

Hope it Helps!!

You have this delegate method which will execute when all the downloading is completed from tha server successfully.Use this method to do the remaining process

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // do something with the data
    // receivedData is declared as a method instance elsewhere
    NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
}

Ok this method gets executed when the all data is recieved.What you should do is to collect all data from -didReceiveData delegate method and then use it to parse in this method.

A must read document for you

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top