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

Networking Requests in iOS

Good afternoon,

I understand the overall process of making a Network Request to an API, how to Parse the returned JSON, populate Data Model Objects, and display the data through the view controller.

I can quite easily read through Networking Code examples and understand what is happening.

What I am struggling with is realising what the Completion Handler actually does. I do understand that it is an escaping closure that is called when the request has finished due to running on a back ground thread.

However when actually looking through code, the 'completion' parameter is used in a function, but doesn't actually seem to do anything at all. It appears to be assigned Data, and Errors returned from the .dataTask / .jsonObject methods or from Error Enums.

I do understand how the closure uses the Response , Data Or Errors. Just don't understand why they are assigned to a 'completion' parameter.

I just can't see what the Completion Handler actually implements or how it is used.

Any would would be greatly appreciated,

Thank you,

Mitch