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
Marc Zovighian
1,664 PointsDid anyone complete the last Extra Credit for the Blog Reader course? (Asynchronous requests)?
This was the last Extra Credit:
"Make a multi-threaded version of the Blog Reader app. Currently, the JSON request is being made on the main thread which makes the user interface unresponsive. Create a multi-threaded version of this app so that it parses and loads the JSON data in a separate thread without blocking interaction with the main UI.
Hint: You can use AFNetworking to make an asynchronous request.
In addition, use SDWebImage to download the images asynchronously."
I have absolutely no idea how to do it
2 Answers
Jason Wayne
11,688 PointsBasically, AFNetworking and SDWebImage are frameworks that you can install into your project. They are not in-built in Xcode, and you can check them out more in the Github on how to install these frameworks. Or you can install them via Cocoapods.
But if you are just looking to not block the main thread, you can consider using Grand Central Dispatch (GCD) or NSOperation. GCD should be much easier to implement as it is lightweight.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// this is the part where you will get do all the connectivity to the JSON and retrieve data
// this is basically moving to the background thread, so that it doesn't do the heavy task on the main thread to block the UI
// check out Apple's Doc about Dispatch_Queue (there's more to than just default such as High and etc..)
dispatch_async(dispatch_get_main_queue(), ^{
// this is the part where you update your UI (labels and etc..)
});
Hope that helps! =]
Sonam Wangyal
2,208 PointsI don't understand the benefits of the Extra Credit, could you please explain it. Please and thanks! :)
Jason Wayne
11,688 PointsJason Wayne
11,688 PointsNot sure why is the grey background color appearing. Just to clarify, in the scope of the block (in between the 2 grey background color), that's where you update your UI, which is done on the main thread once you have done the heavy task in the background.
Marc Zovighian
1,664 PointsMarc Zovighian
1,664 PointsThanks a lot man!