Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>What is particularly confusing in your case is that we're mixing an HTTP request queue (where requests would be sent one after the other) and an operation queue (where random computational work is executed one task after the other).</p> <p>A standard <code>NSURLConnection</code> instance calls its delegate on the main thread, which is OK if you're not doing complex work on the data or on your UI at that time. But say you need to download a big file and write it chunk by chunk as a file on disk, all while scrolling down a table view. Now your scrolling might get choppy when you write data on the disk, blocking the main thread.</p> <p>That's where <code>GCD</code> or its higher level abstraction <code>NSOperationQueue</code> comes into play. To solve this issue, you need to offload your data write calls off of the main thread. You can do that by specifying an <code>NSOperationQueue</code> instance on your <code>NSURLConnection</code> via <code>setDelegateQueue:</code>. This will ensure your delegate, and therefore your write calls, will be called in a background thread. You can also leave the delegate calls on the main thread, and wrap your expensive write calls inside a block which you will then execute off the main thread with <code>dispatch_async</code>. Now <code>NSOperationQueue</code> basically wraps a dispatch queue, and you're avoiding an extra thread switch by using it over a raw dispatch queue so I would recommend the <code>NSOperationQueue</code> solution (which also happens to looks simpler).</p> <p><code>AFNetworking</code> is a great library, and it solves this issue in a third way: it fires off an <code>NSThread</code> which is dedicated to <code>NSURLConnection</code> delegate calls. That's the pre-GCD way of offloading work off the main thread. Although it works, GCD offers a more efficient and good-citizen way of presenting your background work to the system.</p> <p>Last, if you're looking for an HTTP request queue, Cocoa doesn't provide it. You will have to either build a scheduler yourself, or as you already figured it out use <code>AFNetworking</code> which again is a great choice.</p> <p>If you're interested in this topic, GCD has a lot more to offer than just hosting <code>NSURLConnection</code> delegate calls, and I would recommend you read Apple's great <a href="http://developer.apple.com/library/ios/#documentation/General/Conceptual/ConcurrencyProgrammingGuide/Introduction/Introduction.html">Concurrency Programming Guide</a> or watch the excellent <a href="https://developer.apple.com/videos/wwdc/2011/">WWDC 2011 videos</a> on GCD (<em>Blocks and Grand Central Dispatch in Practice</em> and <em>Mastering Grand Central Dispatch</em>).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload