$30 off During Our Annual Pro Sale. View Details »

Comparative Asynchronous Programming

Ash Furrow
February 24, 2017

Comparative Asynchronous Programming

Presented at Playgrounds Conf: http://www.playgroundscon.com/

Ash Furrow

February 24, 2017
Tweet

More Decks by Ash Furrow

Other Decks in Programming

Transcript

  1. Comparative
    Asynchronous
    Programming

    View Slide

  2. View Slide

  3. Agenda
    1. Asynchronous programming is hard.
    2. Swift already supports a few async methodologies.
    3. Swift supports some other paradigms.
    4. But lacks support for others.

    View Slide

  4. Asynchronous Programming:
    Difficult and Subjective

    View Slide

  5. “Normal” programming

    View Slide

  6. let returnValue = someFunctionCall()

    View Slide

  7. Asynchronous programming

    View Slide

  8. Development is all about tradeoffs

    View Slide

  9. let file = readFile()
    ...
    readFile() { file in
    ...
    }

    View Slide

  10. Embrace Tradeoffs

    View Slide

  11. Swift Has Async Built-in. Sorta.

    View Slide

  12. Built-in Async Approaches

    View Slide

  13. Grand Central Dispatch

    View Slide

  14. NSOperationQueue /
    DRBOperationTree

    View Slide

  15. POSIX Threads

    View Slide

  16. Target / Action

    View Slide

  17. Callbacks / Completion Handlers

    View Slide

  18. logIn(with: credentials) { result in
    // Handle login success or failure
    }

    View Slide

  19. Callback Hell

    View Slide

  20. getCredentialsFromUser() { credentials in
    logIn(with: credentials) { result in
    // Handle login success or failure
    }
    }

    View Slide

  21. getCredentialsFromUser() { credentials, error in
    if credentials {
    logIn(with: credentials) { login, error in
    if login {
    // Handle login success
    } else {
    // Handle error
    }
    }
    } else {
    // Handle error
    }
    }

    View Slide

  22. (credentials: Credentials?) -> Void
    (credentials: Credentials?, error: Error?) -> Void
    (result: Result) -> Void

    View Slide

  23. enum Result {
    case success(T)
    case error(Error)
    }

    View Slide

  24. Callback Heaven

    View Slide

  25. Some Async Abstractions are
    Supported in Swift

    View Slide

  26. Promises & Futures

    View Slide

  27. getCredentialsFromUser()
    .flatMap { credentials in
    return login(with: credentials)
    }
    .onSuccess { login in
    // Handle login success
    }
    .onFailure { error in
    // Handle login error
    }

    View Slide

  28. Functional Reactive Programming

    View Slide

  29. getCredentialsFromUser()
    .flatMap { credentials in
    return login(with: credentials)
    }
    .on(next: { login in
    // Handle login success
    }, error: { error in
    // Handle login error
    })

    View Slide

  30. View Slide

  31. gerstureRecognizer.rx
    .event
    .map { recognizer in
    return recognizer.location(in: recognizer.view)
    }
    .bind(to: circle.rx.center)

    View Slide

  32. Actor Model

    View Slide

  33. View Slide

  34. Actor Model

    View Slide

  35. Some Async Abstractions are
    Impossible in Swift

    View Slide

  36. Async / Await

    View Slide

  37. async func logIn() -> Login {
    let credentials = await getCredentialsFromUser()
    return await logIn(with: credentials)
    }

    View Slide

  38. Coroutines and Generator Functions

    View Slide

  39. func fibGenerator*() -> Int {
    var i = 0, j = 1
    repeat {
    let next = i + j
    yield next
    (i, j) = (j, next)
    } while true
    }
    fibGenerator() // returns 1
    fibGenerator() // returns 2
    fibGenerator() // returns 3
    fibGenerator() // returns 5

    View Slide

  40. Wrap Up
    1. There’s no “right way” to do async programming.
    2. Asynchronous abstractions built into Swift aren’t great.
    3. There are some cool things you can do in Swift.
    4. There are other cool things that you can’t do in Swift.

    View Slide

  41. View Slide