Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use async HTTP request over sync HTTP in a separate thread?

I know about the difference between how each works but i want to know in a performance wise point of view (resources inside the iphone).

Lets say I send an asynch request and wait for the delegate to be called. This won't lock my execution thread. But what is the difference of doing this against just sending a synch request in another thread with GCD.

Like this:

dispatch_queue_t findPicsQueue;

findPicsQueue = dispatch_queue_create("FindPicsQueue", NULL);


dispatch_async(findPicsQueue, ^{

    NSData *theResponse = [NSURLConnection sendSynchronousRequest:theRequest
                                                        returningResponse:&response
                                                                    error:&error];

    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;

    if (error) {
       NSLog(@"Error: %@",error)
    }

    if (httpResponse.statusCode == 200) 
    {
        [self parseXMLFile:theResponse]; // Parses Data and modifies picturesFound

        for (PictureData *tmp in picturesFound) {
            NSLog(@"%@",tmp);
        }          
    }  
}

It wont lock my interface since its not being executed in the main thread, but it will lock this specific thread. And I also think GCD runs queues concurrently.

Thanks in advance. I really want to clarify this question.

like image 622
Pochi Avatar asked Aug 10 '12 05:08

Pochi


People also ask

Is HTTP request synchronous or asynchronous?

HTTP is a synchronous protocol: the client issues a request and waits for a response. If you are using non-blocking (aka async) IO, the current thread of the client does not really have to wait, but can do other things (see above).

What is the difference between asynchronous and synchronous request?

Synchronous request — (Default) Where the client blocks and waits for the result of the remote request before continuing execution. Asynchronous request — Where the client continues execution after initiating the request and processes the result whenever the AppServer makes it available.

What is asynchronous HTTP request?

Asynchronous HTTP Request Processing is a relatively new technique that allows you to process a single HTTP request using non-blocking I/O and, if desired in separate threads. Some refer to it as COMET capabilities.

What is a typical use of an asynchronous request-response call?

The asynchronous request-response pattern allows you to tell a sender that the message has been processed and what the outcome or result was. You can leverage this to then build workflows to involve many different services all in a non-blocking way.


1 Answers

If you use NSURLConnection with sendAsynchronousRequest, then almost all processing takes place on the main thread, in particular, the XML parsing will be done on the main thread. Your code example however uses a different thread for processing.

This difference is relevant if you have an iPhone or iPad processor with two cores. Then the XML parsing can run in parallel with some UI activity on the main thread (in your example). So it can be completed earlier compared to running everything on the main thread (sendAsynchronousRequest approach).

For older devices with just one core, only one thread will run at a time and the two approaches should behave almost identical.

like image 182
Codo Avatar answered Sep 28 '22 02:09

Codo