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 trialMarcelo Farjalla
3,241 PointsHow do we know if we are in a background thread vs. the main thread?
In the GCD lesson, Pasan, mentions if we are ever in a background thread but need to update the UI on the main thread we should call the GCD api. I don't really understand how to distinguish if I am in a background thread and/or when I should call that main thread api. Can someone clarify for me? Much appreciated.
1 Answer
Nick Kohrn
38,025 PointsMarcelo,
Yes, that is correct. Whenever you make a network request, Grand Central Dispatch will use a background thread to perform the task.
When you want to return to the main thread in order to update your UI, you can do so by calling this:
dispatch_async(dispatch_get_main_queue(),{
// Add your code to update the UI here
})
Marcelo Farjalla
3,241 PointsMarcelo Farjalla
3,241 PointsI re-watched and Pasan mentions when we return from the getForecast method we are still in the background thread but need to be on the main thread to update the UI. So... anytime I use the async closure method to get something from the server that closure is automatically in the background? Is that correct?