Comparative Asynchronous Programming

0ebf471a3ae8df42a84f93a7efbbdbd0?s=47 Ash Furrow
February 24, 2017

Comparative Asynchronous Programming

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

0ebf471a3ae8df42a84f93a7efbbdbd0?s=128

Ash Furrow

February 24, 2017
Tweet

Transcript

  1. Comparative Asynchronous Programming

  2. None
  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.
  4. Asynchronous Programming: Difficult and Subjective

  5. “Normal” programming

  6. let returnValue = someFunctionCall()

  7. Asynchronous programming

  8. Development is all about tradeoffs

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

    }
  10. Embrace Tradeoffs

  11. Swift Has Async Built-in. Sorta.

  12. Built-in Async Approaches

  13. Grand Central Dispatch

  14. NSOperationQueue / DRBOperationTree

  15. POSIX Threads

  16. Target / Action

  17. Callbacks / Completion Handlers

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

    failure }
  19. Callback Hell

  20. getCredentialsFromUser() { credentials in logIn(with: credentials) { result in //

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

    { login, error in if login { // Handle login success } else { // Handle error } } } else { // Handle error } }
  22. (credentials: Credentials?) -> Void (credentials: Credentials?, error: Error?) -> Void

    (result: Result<Credentials>) -> Void
  23. enum Result<T> { case success(T) case error(Error) }

  24. Callback Heaven

  25. Some Async Abstractions are Supported in Swift

  26. Promises & Futures

  27. getCredentialsFromUser() .flatMap { credentials in return login(with: credentials) } .onSuccess

    { login in // Handle login success } .onFailure { error in // Handle login error }
  28. Functional Reactive Programming

  29. getCredentialsFromUser() .flatMap { credentials in return login(with: credentials) } .on(next:

    { login in // Handle login success }, error: { error in // Handle login error })
  30. None
  31. gerstureRecognizer.rx .event .map { recognizer in return recognizer.location(in: recognizer.view) }

    .bind(to: circle.rx.center)
  32. Actor Model

  33. None
  34. Actor Model

  35. Some Async Abstractions are Impossible in Swift

  36. Async / Await

  37. async func logIn() -> Login { let credentials = await

    getCredentialsFromUser() return await logIn(with: credentials) }
  38. Coroutines and Generator Functions

  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
  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.
  41. None