Tuesday, May 22, 2012

iPhone | Problems with NSURLConnections to Run consecutively (in parallel)

I was trying to get multiple NSURLConnections (async) to run in sequence (consecutively).

The first call worked well (made request & fetched response) while on new async call from "connectionDidFinishLoading" (fyi, I was in a situation that based on first call response) the next call connection doesn't seem to work, as none of the NSURLConnection delegate methods are triggered.

Background threads don't automatically have an active run loop on them. You need to start up the run loop after you create the NSURLConnection in order to get any input from it. Fortunately, this is quite simple:
[[NSRunLoop currentRunLoop] run];
The default mode of NSURLConnection is asynchronous -- it creates and manages a new background thread for you, and calls back to the delegate on the original thread. You therefore don't need to worry about blocking the main thread.

Hope it will work for you, cheers!

No comments:

Post a Comment