1

Following question/statement is limited to my understanding,let me know if i'm wrong here.

From : Issue with GCD and too many threads

Forward to jackslash's answer,i wonder most of the developers have a habbit of using [NSData dataWithContentsOfURL:URL] which certainly does seems to be blocking(until it's done).Which can be ideal if your fetching some small details through URL.But in case of multiple process(i mean while downloading multiple files),GCD has to create many many threads and is not a perfect way to download the data.

So some experts suggested to use GCDs abstraction called [NSURLConnection sendAsynchronousRequest:queue:completionHandler: to handle such situation(and is ideal also).

My only question is how does [NSURLConnection sendAsynchronousRequest:queue:completionHandler: will take the advantage over NSData dataWithContentsOfURL:URL] in terms of creating new threads and saving us from getting blocked?.

Really appreciate any of your documented answer.

Community
  • 1
  • 1
Master Stroke
  • 5,108
  • 2
  • 26
  • 57
  • 1
    Interestingly, my experiments found no difference in thread creation between using `NSData` in GCD and `NSURLConnection` methods. Got a peak of about 70 threads either way while dispatching >100 requests. – Carl Veazey Aug 28 '13 at 08:51
  • @CarlVeazey...but was wondering why experts suggest to go for the NSURLConnection methods...infact in AFNetworking library they prefered the same method... – Master Stroke Aug 28 '13 at 09:41

2 Answers2

3

Your question isn't really about GCD: it's about the best strategy to download many files at the same time.

The best strategy simply is to not do it. Trying to download, say, 100 individual files at the same time is a bad idea, especially if the user is on a cellular connection (which they probably are). You're right to identify that dataWithContentsOfURL is also usually not a great idea.

That said, there's a middle ground here. It would be nice if there was a way to tell iOS "go download X number of files at a time, but no more than that". To do this we can use a NSOperationQueue. An operation queue contains a number of operations and places them into a queue (that in this case is running in the background). For example:

NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
[operationQueue setMaxConcurrentOperationCount:3];
[operationQueue addOperations:@[download1, download2, download3] waitUntilFinished:NO];

Here we've created an operation queue that can support three simultaneous downloads. The advantages of using an operation queue should be obvious. Many developers choose to use a library like AFNetworking to help them manage downloading, and there are lots of guides around to using both this and NSURLConnection in an operation queue.

lxt
  • 31,146
  • 5
  • 78
  • 83
  • Thanks for detailed information...yes,i agree wih you...just want to extract some more information from you about,how does NSOperationQueue makes an impact...I mean is it faster and efficient enough?...as you said it runs in background... – Master Stroke Aug 28 '13 at 09:54
  • 1
    `NSOperationQueue` helps you because it lets you control the *maximum* number of concurrent operations. Say you have 100 individual files you want to download. Obviously it would be a very bad idea to create 100 unique threads. But with an operation queue you can specify a maximum number of concurrent operations (say, 3 at a time), and be sure you won't exceed that. – lxt Aug 28 '13 at 10:43
1

sendAsynchronousRequest:queue:completionHandler: doesn't use GCD for the actual downloading. The queue you pass in is only used to execute the completion block. The download itself will be using low level asynchronous socket APIs in its internal implementation so there won't be any blocking or unnecessary thread creation.

Mike Weller
  • 45,401
  • 15
  • 131
  • 151