You haven't shared how you're running it "on a different thread", but the typical problem is if you are using dispatch queues or operation queues, your connection is running asynchronously, itself, and therefore the dispatched operation is completing and getting released and you're losing your connection.
A couple of possible solutions:
You can perform your network operations synchronously in this background operation of yours (this is the only time you should do synchronous network operations). This is the simplest solution, though you haven't explained what you're doing with your
NSURLConnection
, so this technique may or may not work for you. But if you're just trying to download something from a URL, you can do:NSData *data = [NSData dataWithContentsOfURL:url options:0 error:&error];
This approach doesn't work if you're doing any a little more complicated that requires the
NSURLConnectionDataDelegate
methods, such challenge-response authentication or if you are streaming usingdidReceiveData
to reduce your app's memory footprint or for performances reasons, etc. But if you're just trying to download data from a remote server (e.g. retrieving an image, an XML/JSON feed, etc.), this is easiest.In a similar vein (i.e. you don't need the
NSURLConnectionDataDelegate
methods), but you're you're creating some richNSURLRequest
for your connection, then you can use eithersendAsynchronousRequest
orsendSynchronousRequest
.If you need the
NSURLConnectionDataDelegate
calls, you can use thesetDelegateQueue
(designating aNSOperationQueue
) orscheduleInRunLoop
(designating aNSRunLoop
), and it will automatically dispatch the connection updates to the appropriate queue/runloop. Just make sure toinitWithRequest
using thestartImmediately
option ofNO
, set the delegate queue or runloop, and thenstart
the connection. With this technique, you preserve the full richness ofNSURLConnectionDataDelegate
if you absolutely need it.Alternatively, if you're not using operation queues, you could also keep your background operation alive until the connection is done. This approach gives you synchronous behavior in you background operation (keeping your connection alive), while preserving the
NSURLConnectionDataDelegate
methods. This technique is demonstrated by Apple's XMLPerformance Sample (see thedownloadAndParse
method of CocoaXMLParser.m and LibXMLParser.m), where they initiate theNSURLConnection
and then use the following construct to keep the background operation alive until theNSURLConnectionDataDelegate
methods end up setting thedone
instance variable:do { [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]; } while (!done);
I confess that I find this final approach vaguely dissatisfying and would lean towards the other alternatives, depending upon what flexibility and functionality you need from your NSURLConnection
. For us to provide more meaningful counsel, you just need to provide more information about (a) the sort of work you're doing in your NSURLConnectionDataDelegate
methods; and (b) which technology you're using to run your code in the background.
For a few more options, also feel free to see GCD and async NSURLConnection.