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?

jtrim
  • 3,465
  • 4
  • 31
  • 44

1 Answers1

3

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
  • 102,279
  • 44
  • 260
  • 354
  • Just to clarify, basically you're saying it may not even be worth the trouble? – jtrim Oct 30 '09 at 14:27
  • 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 Oct 30 '09 at 14:33
  • 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 Oct 30 '09 at 14:36
  • 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 Oct 30 '09 at 14:40