Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialThomas Nilsen
14,957 PointsUsing blocks + dispatch_async VS. NSURLSession
Say I want to Parse a JSON response. They both should be able to achieve this. But, is there a time where one would be more useful than the other, or is it just a matter of preference?
2 Answers
Amit Bijlani
Treehouse Guest TeacherNSURLSession
a high level API to make it easy to perform HTTP requests. Grand Central Dispatch (GCD) is a concurrency API. You can use GCD to achieve asynchronous calls but then you have to do all the network management manually.
Christopher Hall
9,052 PointsFirst of all, NSURLSession is a new class available in iOS 7, so if you need any backwards compatibility, then use NSURLConnection instead. Here is a link which explains the difference between the two. Sorry, I can't give a detailed reason why one might be better than the other, but hopefully this helps.
Thomas Nilsen
14,957 PointsThomas Nilsen
14,957 PointsSo there is no advantages to use GCD as opposed to NSURLSession or NSURLConnection, just a matter of preference?
Amit Bijlani
Treehouse Guest TeacherAmit Bijlani
Treehouse Guest TeacherYou should not be using
NSURLConnection
anymore. As for GCD vsNSURLSession
it really depends what you are trying to achieve. If you are making an HTTP request to get data then you should useNSURLSession
. If you just want to have concurrency to perform several tasks that having nothing to do with networking calls then by all means use GCD. Different tasks call for different tools.Thomas Nilsen
14,957 PointsThomas Nilsen
14,957 PointsI see. The main reason I asked, was because I recently came across an example were the Flickr API was used. That tutorial used GCD for requesting the data, and parsing the response. That made me wonder.
Justin Cannon
3,465 PointsJustin Cannon
3,465 PointsTo extend this question - if I want to download dozens of resources (images and sound, say) in the background of my app, I probably shouldn't just make dozens of NSURLSession requests at once, correct? I've been looking into NSOperationQueues. Could NSURLSessions inside of custom NSOperations be a good solution, or is that overkill?
Amit Bijlani
Treehouse Guest TeacherAmit Bijlani
Treehouse Guest TeacherBy default
NSURLSession
always makes request in a background queue. You can create your own custom configuration to use your own NSOperationQueue but if all you are doing is downloading resources then that might be overkill. For example, if you were to build a web browser and had multiple tabs then each tab would have it's own session configuration.Justin Cannon
3,465 PointsJustin Cannon
3,465 PointsGreat, thank you Amit. NSURLSession looks great - but iOS7 looks like it is at 80-85% adoption rate. It would be unfortunate if 1 in 5 people with an iphone can't use my app. I don't think there was a lesson on backwards compatibility. I see you've strongly recommended NSURLSession above, but how do you approach compatibility?
Amit Bijlani
Treehouse Guest TeacherAmit Bijlani
Treehouse Guest TeacherIf you are worried about backward compatibility then check out AFNetworking.