Reusing an instance of NSURLConnection
Asked Answered
M

1

7

I'm using an instance of NSURLConnection on the iPhone to request data from a server, managed by a delegate as usual. The requests are quite frequent (maybe once every 2 minutes say) and have a common and fixed URL. Rather than seeing the good instance of NSURLConnection being released after each download and then a new one being created:

  1. Is there any worth in retaining the first connection and reusing it? (I'd hope so, one good authentication should be worth a thousand.)

  2. If so, how do I reuse it? The standout method in the docs is -start but this seems to crash the app when called on an already used (and non-nil) instance of NSURLConnection. [The docs do say -start "causes the receiver to begin loading data, if it has not already."]

In case it's of help with regard to the above questions, I am (was!) proposing:

if (connection_ == nil)
   {
    connection_ = [NSURLConnection connectionWithRequest:request
                                                delegate:self];
   }
  else
   {
    [connection_ start];
   }
Misbeliever answered 4/3, 2011 at 0:42 Comment(3)
This is a slight duplicate, see #887310 . Still no good answer though. It looks like you need to concoct your own class for this task.Misbeliever
It would seem that one of the answers to that question is suggesting an HTTP stream rather than a use-once connection. Keeping a stream open and only using it every 2 minutes wouldn't be a great idea. I don't really see a problem with releasing and recreating the connection object. Is this not a pre-optimisation?Ankylosaur
@J: A stream is not good for me. The frequency I mentioned is an average, and when the request is needed is driven by the user's activity. I download 20 items, and request another 20 when the user gets down to 10. Currently I'm doing this with a new NSURLConnection each time and wonder if there is a better way. If need be, I'm still very interested in a solution to an abstraction of the question.Misbeliever
A
3

The docs seems to say that the URL connection retains it's delegate (unconventional, but necessary in this case) and then releases it when the connection finishes loading, fails or is cancelled.

The problem is that the delegate isn't a settable property on NSURLConnection and so you can't reset it after it's been released. This pretty much renders the URL connection useless after it has run once, requiring you to release and recreate it if you want to do it again.

Ankylosaur answered 4/3, 2011 at 1:34 Comment(4)
@J: This what I gathered too. I don't know about alternatives to NSURLConnection here, maybe ASI [ allseeing-i.com/ASIHTTPRequest ] etc.?Misbeliever
I havn't used AIHTTPRequest, but I believe it is built upon NSURLConnection so even if you can reuse an instance of one of those, it would still be releasing and recreating the url connection beneath the hood.Ankylosaur
@J: Is it possible to create an authenticated NSURLConnection ahead of time, and delay carrying out the request? (i.e. load the gun, fire when ready.)Misbeliever
not with NSURLConnection by itself. Maybe ASIHTTPRequest can do it, but I don't know for sure. Have you looked into the NSURLCredential caching?Ankylosaur

© 2022 - 2024 — McMap. All rights reserved.