I understand that it must be taking care of the ugliness of launching a thread to make the network request, then probably calling performSelectorOnMainThread:
with my delegate method.
I know how to use it when doing iOS programming, and it works great. However, I'd like to know how I would make it work doing in the context of (for example) a command line utility, where there is no UIApplication with the event processing etc.
I've tried it, and it seems that the program exits as soon as the asynchronous call returns, before the delegate methods can get called. I pretty much want a deeper understanding of how it works.
Per the docs, the connection's delegate methods get called on the same thread that the connection was started from. So, to keep that thread running until the connection has time to do its stuff:
Then the delegate can stop the run loop when the connection says it's finished:
The delegate method calls are scheduled on the runloop of the thread that initiated the connection. So it's not necessarily the main thread. You can do this from any thread. But you must have a run loop that is running long enough to handle those delegate messages.
And by default, the run loop has to be running in its default mode for those delegate methods to fire. So this is why, for example, by default if you start an async connection from the main thread, your delegate methods will not fire while the user is scrolling a tableview or something like that.
But you can change this by scheduling the delegate callbacks on any other run loop you want and/or any run loop mode you want using -[NSURLConnection scheduleInRunLoop:forMode:]
I hope that is what you were asking?