views:

112

answers:

1

I've got an app that will be doing a fair amount of communication with a server over HTTP, and these connections may be overlapping. I'm planning to load the data asynchronously. I understand that there is a performance hit associated with allocating memory, so I figured the smart thing to do would be to keep a set of available connection objects so that every time I needed to communicate with the server there would be a good chance that I wouldn't have to allocate a new connection, just reuse an idle one. However, there doesn't seem to be a way to fire up an existing connection with a new request object. Is this something I'm missing, or am I barking up the wrong tree?

+2  A: 

Obey the golden rule of performance optimization: Measure first. It is quite probable that the performance hit caused by allocating a new connection object is going to be negligible.

zoul
Just to clarify, basically you're saying it may not even be worth the trouble?
jtrim
Exactly. If you want to be sure, you can even set up a testing target for such performance assumptions, allocate as many connections as you are going to need and `STAssert` that you can do that in decent timeframe.
zoul
Cool, thanks for the insight. I looked into it a bit more and found a few instances where it's said that NSURLConnection objects can't be reused, so apparently they're not meant to be used in that manner anyway.
jtrim
Yes, because reusing objects often adds a lot of cruft into the code. Same goes for `NSTimer` and other classes, it’s simply not worth it.
zoul