1

The app I'm making must fetch a lot of data from a lot of different urls. Let's say, roughly 200 urls, each with 50kb-300kb of data (images).

Right now, I'm basically opening all of those url connections at once and letting them finish as they finish.

However... I'm not really sure exactly what NSURLConnection is doing behind the scenes. Would I be better off setting up an NSURLConnection queue that allowed say.... 5 connections at once, and as one connection finished, another one would start?

In other words, is there an advantage to queueing a large number of NSURLConnections, starting new connections as the old ones finish, as opposed to making them all at the same time?

MikeS
  • 3,891
  • 6
  • 35
  • 51

3 Answers3

3

Basically opening them all at once is going to thrash the thread scheduler it will end up being slower than doing it in batches, and setting a limit for how many can be opened at once. The best way to do this is to create a Network Manager to manage this connections.

Oscar Gomez
  • 18,436
  • 13
  • 85
  • 118
  • Is there a general number of NSURLConnections to use simultaneously in this scenario that seems to work well? – MikeS Feb 22 '13 at 16:20
  • Do know if `ASIHTTPRequest` or `AFNetworking` handle this already? – Mike D Feb 22 '13 at 16:22
  • I usually set a 5 - 10 limit. But to determine the ideal number for your app the best way would be to profile it. I do not use ASIHTTPRequest but I would expect it to handle this by default. – Oscar Gomez Feb 22 '13 at 16:24
1

Based on my little experience, and what I could observe in my own apps, I would say that queueing is the best way to do. Have a look to NSOperationQueue (a very great tuto is available here) It will help you to manage several async connections in a dedicated queue.

GUL
  • 207
  • 1
  • 8
1

This is a general question rather than iOS and it is true for any online project (e.g. websites). Most download managers will allow you to have 5-10 simultaneous downloads and queue others, if you want to take their advice.

I'd suggest using NSOperationQueue and let iOS do the optimisation for you. And do not forget to set timeout on your requests so blocked requests do not take up from your simultaneous quota.

Ege Akpinar
  • 3,266
  • 1
  • 23
  • 29
  • Well, I figured it was iOS specific because I didn't know if NSURLConnection was doing anything along this sort of line behind the scenes. – MikeS Feb 22 '13 at 16:27