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 trial

iOS

Patrick Cooney
Patrick Cooney
12,216 Points

NSData dataWithContentsOfURL: vs. NSURLConnection

First some background on my app. It's a single page, single view app with no buttons, no gestures, etc. You open the app, it updates the data, you look over the data, you close the app, that's it. It grabs some data from Parse (thanks for the idea of using a BaaS, btw. I was going to write a whole custom back-end in ruby until I saw parse in the self destructing app project.) using the Parse framework which I trust is well designed.

I also grab some data from a weather API. Here's where my question comes in. I'm using NSData due to the simplicity of it. I figure it doesn't much matter if it's a synchronous call and the data takes a while to load as there is literally nothing else to do in the app other than look at the data it loads. If it's going to time out with a NSData, I assume it is going to time out with a NSURLConnection. Knowing what the app is and does, is there any reason to be using an NSURLConnection over NSData in my specific case?

2 Answers

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

When you use NSData dataWithContentsOfURL it executes on the main thread which means that the user will not be able to interact with the App nor can you update the UI. NSURLConnection executes in a separate thread so you can display a progress indicator while the data is being downloaded. With iOS 7 it is recommended that you use NSURLSession.

Patrick Cooney
Patrick Cooney
12,216 Points

Thanks for the explanation. I'll be sure to use NSURLSession going forward. Since this app literally has no interaction and the UI only updates if it can successfully retrieve data I'll probably leave it for this one. I just wanted to get comfortable with parsing JSON responses. I also have to use 6.1 for this app since it's for a class that began before iOS 7's release.