Appendix C. How Asynchronous Works
A very common beginner mistake is rooted in a failure to understand what it means for code to run asynchronously. It means that your code runs out of order. Consider:
func doSomeNetworking() { // ... prepare url ... let session = URLSession.shared let task = session.downloadTask(with:url) { loc, resp, err in // ... completion function body goes here ... } task.resume() }
The method downloadTask(with:completionHandler:)
calls its completion function asynchronously — because it calls it when the networking finishes, and networking takes time. The order in which the chunks of code run is the numerical order of the numbered lines:
Get Programming iOS 10 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.