Unidirectional Data Flow in Swift

Unidirectional Data Flow in Swift

Unidirectional Data Flow in Swift built with the Swift Flow Framework.

De23af005c790b22f8ce4d201e6ca027?s=128

Benjamin Encz

December 15, 2015
Tweet

Transcript

  1. BENJAMIN ENCZ @BENJAMINENCZ 1 — Unidirectional Data Flow in Swift

    | @benjaminencz | SLUG @ Realm, December 2015
  2. UNIDIRECTIONAL DATA FLOW IN SWIFT & THE END OF MODEL-VIEW-CONTROLLER

    2 — Unidirectional Data Flow in Swift | @benjaminencz | SLUG @ Realm, December 2015
  3. 1979 3 — Unidirectional Data Flow in Swift | @benjaminencz

    | SLUG @ Realm, December 2015
  4. DOES ANYONE EVEN UNDERSTAND MVC? 4 — Unidirectional Data Flow

    in Swift | @benjaminencz | SLUG @ Realm, December 2015
  5. 5 — Unidirectional Data Flow in Swift | @benjaminencz |

    SLUG @ Realm, December 2015
  6. 6 — Unidirectional Data Flow in Swift | @benjaminencz |

    SLUG @ Realm, December 2015
  7. 2016 7 — Unidirectional Data Flow in Swift | @benjaminencz

    | SLUG @ Realm, December 2015
  8. MVC IS NOT A HOLISTIC APPLICATION ARCHITECTURE 8 — Unidirectional

    Data Flow in Swift | @benjaminencz | SLUG @ Realm, December 2015
  9. PROBLEM #1: VIEW CONTROLLERS ARE MICROMANAGERS IMAGE CREDITS: NAKEDPASTOR.COM 9

    — Unidirectional Data Flow in Swift | @benjaminencz | SLUG @ Realm, December 2015
  10. func userLoggedInWithUsername(username: String, password: String) { apiClient.authenticateUser(username, password: password) {

    response, error in if (error == nil) { let nextViewController = ... navigationController.pushViewController(nextViewController) } else { showErrorMessage(error) } } } 10 — Unidirectional Data Flow in Swift | @benjaminencz | SLUG @ Realm, December 2015
  11. PROBLEM #2: WHERE IS STATE? 11 — Unidirectional Data Flow

    in Swift | @benjaminencz | SLUG @ Realm, December 2015
  12. WHERE IS STATE? > Currently Active Views > Currently Active

    View Controllers > Database > Singletons? 12 — Unidirectional Data Flow in Swift | @benjaminencz | SLUG @ Realm, December 2015
  13. HOW DO I PASS INFORMATION BETWEEN VIEW CONTROLLERS? 13 —

    Unidirectional Data Flow in Swift | @benjaminencz | SLUG @ Realm, December 2015
  14. 14 — Unidirectional Data Flow in Swift | @benjaminencz |

    SLUG @ Realm, December 2015
  15. PROBLEMS I HAVE WITH MVC > View Controllers need to

    know business logic details 15 — Unidirectional Data Flow in Swift | @benjaminencz | SLUG @ Realm, December 2015
  16. PROBLEMS I HAVE WITH MVC > View Controllers need to

    know business logic details > View Controllers need to manage significant amount of state (almost anything that is not stored in the DB) 16 — Unidirectional Data Flow in Swift | @benjaminencz | SLUG @ Realm, December 2015
  17. PROBLEMS I HAVE WITH MVC > View Controllers need to

    know business logic details > View Controllers need to manage significant amount of state (almost anything that is not stored in the DB) > State management & propagation happens ad-hoc 17 — Unidirectional Data Flow in Swift | @benjaminencz | SLUG @ Realm, December 2015
  18. PROBLEMS I HAVE WITH MVC > View Controllers need to

    know business logic details > View Controllers need to manage significant amount of state (almost anything that is not stored in the DB) > State management & propagation happens ad-hoc > Very difficult to build a mental model of how an application works 18 — Unidirectional Data Flow in Swift | @benjaminencz | SLUG @ Realm, December 2015
  19. MEET REDUX! 19 — Unidirectional Data Flow in Swift |

    @benjaminencz | SLUG @ Realm, December 2015
  20. 20 — Unidirectional Data Flow in Swift | @benjaminencz |

    SLUG @ Realm, December 2015
  21. 21 — Unidirectional Data Flow in Swift | @benjaminencz |

    SLUG @ Realm, December 2015
  22. REDUCERS: (STATE, ACTION) -> STATE 22 — Unidirectional Data Flow

    in Swift | @benjaminencz | SLUG @ Realm, December 2015
  23. ACTIONS: DECLARATIVE DESCRIPTION OF A STATE CHANGE 23 — Unidirectional

    Data Flow in Swift | @benjaminencz | SLUG @ Realm, December 2015
  24. SWIFT FLOW UNFINISHED OPEN SOURCE REDUX IMPLEMENTATION IN SWIFT 24

    — Unidirectional Data Flow in Swift | @benjaminencz | SLUG @ Realm, December 2015
  25. 25 — Unidirectional Data Flow in Swift | @benjaminencz |

    SLUG @ Realm, December 2015
  26. struct AppState: StateType, HasNavigationState { var counter: Int = 0

    var navigationState = NavigationState() } 26 — Unidirectional Data Flow in Swift | @benjaminencz | SLUG @ Realm, December 2015
  27. func newState(state: AppState) { counterLabel.text = "\(state.counter)" } @IBAction func

    increaseButtonTapped(sender: UIButton) { mainStore.dispatch( Action(CounterActionIncrease) ) } @IBAction func decreaseButtonTapped(sender: UIButton) { mainStore.dispatch( Action(CounterActionDecrease) ) } 27 — Unidirectional Data Flow in Swift | @benjaminencz | SLUG @ Realm, December 2015
  28. struct CounterReducer: Reducer { func handleAction(state: AppState, action: Action) ->

    AppState { var state = state switch action.type { case CounterActionIncrease: state.counter += 1 case CounterActionDecrease: state.counter -= 1 default: break } return state } } 28 — Unidirectional Data Flow in Swift | @benjaminencz | SLUG @ Realm, December 2015
  29. TIME TRAVEL! 29 — Unidirectional Data Flow in Swift |

    @benjaminencz | SLUG @ Realm, December 2015
  30. DEMO 30 — Unidirectional Data Flow in Swift | @benjaminencz

    | SLUG @ Realm, December 2015
  31. REAL WORLD EXAMPLES? 31 — Unidirectional Data Flow in Swift

    | @benjaminencz | SLUG @ Realm, December 2015
  32. ENTIRE APP STATE IN ONE DATA STRUCTURE? 32 — Unidirectional

    Data Flow in Swift | @benjaminencz | SLUG @ Realm, December 2015
  33. 33 — Unidirectional Data Flow in Swift | @benjaminencz |

    SLUG @ Realm, December 2015
  34. WHAT ABOUT ASYNC? 34 — Unidirectional Data Flow in Swift

    | @benjaminencz | SLUG @ Realm, December 2015
  35. func fetchUserList -> ActionCreator { return { state, store in

    self.apiClient.fetchUsers() { users in store.dispatch( SetUsers(users) ) } } } 35 — Unidirectional Data Flow in Swift | @benjaminencz | SLUG @ Realm, December 2015
  36. 36 — Unidirectional Data Flow in Swift | @benjaminencz |

    SLUG @ Realm, December 2015
  37. CHALLENGES > UIKit > Encoding / Decoding > Restricing Access

    to Global State 37 — Unidirectional Data Flow in Swift | @benjaminencz | SLUG @ Realm, December 2015
  38. WHY SWIFT FLOW? > Separation of concerns 38 — Unidirectional

    Data Flow in Swift | @benjaminencz | SLUG @ Realm, December 2015
  39. WHY SWIFT FLOW? > Separation of concerns > Decoupling of

    intent and implementation 39 — Unidirectional Data Flow in Swift | @benjaminencz | SLUG @ Realm, December 2015
  40. WHY SWIFT FLOW? > Separation of concerns > Decoupling of

    intent and implementation > Clear, Declarative API 40 — Unidirectional Data Flow in Swift | @benjaminencz | SLUG @ Realm, December 2015
  41. WHY SWIFT FLOW? > Separation of concerns > Decoupling of

    intent and implementation > Clear, Declarative API > Predictable, explicit state 41 — Unidirectional Data Flow in Swift | @benjaminencz | SLUG @ Realm, December 2015
  42. WHY SWIFT FLOW? > Separation of concerns > Decoupling of

    intent and implementation > Clear, Declarative API > Predictable, explicit state > Program has a shape 42 — Unidirectional Data Flow in Swift | @benjaminencz | SLUG @ Realm, December 2015
  43. WHY SWIFT FLOW? > Separation of concerns > Decoupling of

    intent and implementation > Clear, Declarative API > Predictable, explicit state > Program has a shape > Automatic state propagation 43 — Unidirectional Data Flow in Swift | @benjaminencz | SLUG @ Realm, December 2015
  44. CREDITS > Gerald Monaco (@devknoll) for introducing me to Redux

    > Dan Abramov (@dan_abramov) for building Redux > Jake Craige (@jakecraige) for feedback and support during implementation 44 — Unidirectional Data Flow in Swift | @benjaminencz | SLUG @ Realm, December 2015
  45. SPEAKERDECK.COM/BENJAMIN_ENCZ GITHUB.COM/SWIFT-FLOW @BENJAMINENCZ 45 — Unidirectional Data Flow in Swift

    | @benjaminencz | SLUG @ Realm, December 2015