What is the differance between adding a operation which make a synchronous NSURLConnection request in NSOperationQueue ( or synchronous request from a thread ( not main thread)) AND making a asynchronous request from the main thread ?
Both will not block main thread so UI will remain responsive but is there any advantage of using one over other? I know in later method i can track request progress etc but assume that progress and other HTTP stuff is not important here.
Asynchronous requests are scheduled on the run loop and setup as a run loop source, triggering the code automatically only when there is data received from the network (as any socket source).
Synchronous requests running on a
NSThreadmonopolizes a thread to monitor the incoming data, which is in general quite overkill.You can always cancel an
NSURLConnectioneven if it has been executed asynchronously, using thecancelmethod.I bet using the new API that allows to send an asynchronous request on an
NSOperationQueue(+sendAsynchronousRequest:queue:completionHandler:) uses GCD under the hood anddispatch_source_create, or something similar, so that it behave the same way as when anNSURLConnectionis scheduled on the run loop, avoiding using an additional thread (watch the WWDC’12 videos that explains why threads are evil and their usage should be minimized), the difference only being that allows you to use a block to be informed upon completion instead of using the delegate mechanism.Some years ago I created a class that embedded
NSURLConnectionasynchronous calls and delegate management into a nice block API (see OHURLLoader on my github) that makes it easier to use (feel free to take a look). I bet the new API that usesNSOperationQueues uses the same principle, still doing asynchronous requests on the runloop but allowing you to use blocks instead of having to implement a delegate.