Question

NSURLConnection's documentation says that NSURLConnection is run on a separate thread and that delegate methods are called on a thread that started the connection.

I have more that one NSURLConnection objects wrapped in "URLDownload < NSURLConnectionDelegate >" objects which I run in parallel.

While pausing my program (at some time I put a breakpoint in it) and looking into Debug Navigator, I see only one com.apple.NSURLConnectionLoader thread.

Here is how I think this works:

  1. I create 5 URLDownload object (each of them has its own NSURLConnection)
  2. 5 threads are created (one for each NSURLConnection)
  3. Each NSURLConnection calls it's delegate methods on the thread that started them.

So there are 5 threads (one for each NSURLConnection) but their data (didReceiveData... method and others) is handled on a single thread.

Did I get this right?

Was it helpful?

Solution

Asynchronous NSURLConnection's will do there work off the main thread, that is all you need to know.

Threads use a lot of resources relative to the amount of work of a network connection, so 1 asynchronous NSURLConnection may use 1 background thread, 2 asynchronous NSURLConnections may use 2 background threads, but 100 asynchronous NSURLConnections probably won't use 100 background threads.

NSURLConnection is a black box that will manage the optimum amount for you. This could be based on how many processors you have, your network speed, etc (NB i'm not saying i know how it works under the hood, just that you don't need to know - it will do the right thing. One thread per connection would be a pretty horrible way todo it).

The delegate methods (didReceiveData, etc) get called on the thread that started the connection, so that isn't the same as saying that they are called on a single thread. If you start 5 connections on five different threads you will get the callbacks on five different threads.

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