Unidirectional Data Flow in Swift

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest. →

Unidirectional Data Flow in Swift

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

Benjamin Encz

December 15, 2015

Annual Pro sale · Save $30

Have presentations of your own?

Share with private links, password protection, and more control over your work.

Transcript

45 slides
  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. DOES ANYONE EVEN UNDERSTAND MVC? 4 — Unidirectional Data Flow

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

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

    — Unidirectional Data Flow in Swift | @benjaminencz | SLUG @ Realm, December 2015
  6. 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
  7. PROBLEM #2: WHERE IS STATE? 11 — Unidirectional Data Flow

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

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

    Unidirectional Data Flow in Swift | @benjaminencz | SLUG @ Realm, December 2015
  10. PROBLEMS I HAVE WITH MVC > View Controllers need to

    know business logic details 15 — Unidirectional Data Flow in Swift | @benjaminencz | SLUG @ Realm, December 2015
  11. 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
  12. 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
  13. 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
  14. MEET REDUX! 19 — Unidirectional Data Flow in Swift |

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

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

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

    — Unidirectional Data Flow in Swift | @benjaminencz | SLUG @ Realm, December 2015
  18. struct AppState: StateType, HasNavigationState { var counter: Int = 0

    var navigationState = NavigationState() } 26 — Unidirectional Data Flow in Swift | @benjaminencz | SLUG @ Realm, December 2015
  19. 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
  20. 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
  21. TIME TRAVEL! 29 — Unidirectional Data Flow in Swift |

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

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

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

    | @benjaminencz | SLUG @ Realm, December 2015
  25. 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
  26. CHALLENGES > UIKit > Encoding / Decoding > Restricing Access

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

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

    intent and implementation 39 — Unidirectional Data Flow in Swift | @benjaminencz | SLUG @ Realm, December 2015
  29. 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
  30. 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
  31. 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
  32. 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
  33. 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

More Decks by Benjamin Encz

Other Decks in Programming