Getting data from the web is a pretty common operation for apps these days. Part of the marvel of the iPhone is that you’re always connected. Now, often we make network requests and don’t worry much about them until they’re done. What you might not know is where the code to handle the request is running. And by default that place often ends up being the main thread.
AFNetworking takes care of this issue for you. If you’re curious how it works, or can’t use AFNetworking then read on.
There’s 3 basic ways to make requests to get data from a URL. All of them involve the class
sendSynchronousRequest:returningResponse:error:– This will block until the request is complete. Which means if you run it on the main thread, the main thread will be blocked. So basically you probably don’t want to use this method.
sendAsynchronousRequest:queue:completionHandler:– This blocks until the request is complete and runs the block
queuewhen it’s done. So this seems useful, but might cause performance problems when you aren’t expecting any.
So we’ll be focusing on the traditional method, since it is the only one that really does what we want.
If you’re already just calling
start you might wonder why it’s not
causing big issues. Well, if you’ve programmed the delegate callbacks well,
then you’re only blocking the main thread very briefly. iOS is already pretty
smart about how often it calls the delegate. Problems might come up if you
start doing a lot of work from the callback methods, often times in the
completion callback. You might notice problems like the UI being unresponsive,
or animation jitter. Thankfully, we can fix this.
Telling NSURLConnection Where to Work
There are three ways to tell
NSURLConnection where to run its callbacks.
start from Another Thread
This sounds easy enough, that’s why we have
dispatch_async right? Well,
sadly, no that doesn’t work. You see GCD queues are not the same as
threads1, and when you call
dispatch_async for a particular queue all
it guarantees is what order it will start relative to other blocks in the
queue. GCD will create and destroy threads frequently and there’s a good
chance2 that the thread you call
start from will stop existing
immediately afterwards. This results in the delegate not receiving any
Instead you need to run
start from a specific thread. Doing this is very
similar to the next method, which provides more flexibility anyhow, and only
two more lines of code.
For this to work you still need to have code run in a specific thread, and then
[NSRunLoop currentRunLoop] from within that, and
pass it to
scheduleInRunLoop:forMode:. Because of compatibility, this is
how AFNetworking manages it’s callbacks asynchronously. Here’s a bit of the
code from AFNetworking:
1 2 3 4 5 6 7 8 9 10 11
If you look a few lines up from this snippet, you see that calling
an AFURLConnectionOperation runs this bit of code on a separate thread.
You might ask why even call
scheduleInRunLoop:forMode: and not just call
start. Well if you wanted to configure an
NSURLConnection in one
place, and then call start from somewhere else, even on a different thread,
this would ensure that it runs on the network thread. So by setting the run
loop in advance, you can be flexible about when and where you start the
There’s a dirty shortcut
allowing you to do this when called within a
dispatch_async block, but it’s
not a very clean solution, and I don’t recommend it.
The easiest way to manage where your callbacks are run on iOS 5.0 and up is
setDelegateQueue:. You simply create an
NSOperationQueue and assign
it as the
delegateQueue for the
NSURLConnection. If I have a connection
manager class, this takes two bits of code typically:
1 2 3 4 5
1 2 3 4 5
This removes the need to manage a thread or run loop, and you can focus on the important parts of the code.
Why Would I Use This Instead of AFNetworking?
Well, not very often. The big reason for writing your own connection delegate
is usually to have a single place where response handling code exists, and not
where the call is. You can do this with AFNetworking, but for simple tasks it
might make more sense to handle it yourself. The basic
delegate code is actually quite simple, so might make more sense than adding
You also might need to support iOS 4.3, and don’t want to use an old release of AFNetworking.
Finally, you might be constrained by the MIT license. Or more likely, your lawyers don’t want you using it.
Update 9/15/2013: Fixed references to
sendAsynchronousRequest:queue:completionHandler: since it is actually a poor method to use.