Skip to Content
Programming iOS 10
book

Programming iOS 10

by Matt Neuburg
November 2016
Beginner to intermediate content levelBeginner to intermediate
800 pages
30h 12m
English
O'Reilly Media, Inc.
Content preview from Programming iOS 10

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 1
    let task = session.downloadTask(with:url) { loc, resp, err in 2
        // ... completion function body goes here ... 4
    }
    task.resume() 3
}

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:

1

The code before the call.

2

The call itself.

3

The code after the call, including the return from the surrounding function doSomeNetworking ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Programming iOS 12

Programming iOS 12

Matt Neuburg
Programming iOS 11

Programming iOS 11

Matt Neuburg
Programming iOS 13

Programming iOS 13

Matt Neuburg

Publisher Resources

ISBN: 9781491970157Errata Page