Reusing an instance of NSURLConnection

I am using an instance of NSURLConnection on the iPhone to request data from the server that the delegate manages, as usual. Requests are quite common (possibly once every 2 minutes) and have a common and fixed URL. Instead of seeing that after each download, a good instance of NSURLConnection is issued, and then a new one is created:

  • Does it make sense to save the first connection and reuse it? (Hopefully one good authentication should cost a thousand.)

  • If so, how to reuse it? The highlighted method in the -start docs, but this seems to cause the application to crash when calling an already used (and not null) NSURLConnection instance. [Documents say -start "forces the receiver to start loading data if it has not already been." ]

If this helps in relation to the above issues, I (was!) Suggest:

 if (connection_ == nil) { connection_ = [NSURLConnection connectionWithRequest:request delegate:self]; } else { [connection_ start]; } 
+7
source share
1 answer

The docs seem to say that the URL connection retains its delegation (unconventional, but necessary in this case), and then releases it when the connection finishes loading, fails, or is canceled.

The problem is that the delegate is not a settable property in NSURLConnection, so you cannot reset after its release. This pretty much makes the URL connection useless once it starts once, requiring you to release and recreate it if you want to do it again.

+3
source

All Articles