Upgrade to Pro — share decks privately, control downloads, hide ads and more …

State Management & Unidirectional Data Flow in Swift

Nikolas
March 17, 2017

State Management & Unidirectional Data Flow in Swift

Nikolas

March 17, 2017
Tweet

Other Decks in Programming

Transcript

  1. @nikolasburk State Management & Unidirectional Data Flow State Management &

    Unidirectional Data Flow “Where is truth in your application?” Andy Matuschak, WWDC 2014
  2. Nikolas Burk @nikolasburk • Developer at Graphcool • Experienced with

    iOS • Passionate about programming paradigms and architecture
  3. 1. State Management 2. Unidirectional Data Flow & ReSwift @nikolasburk

    State Management & Unidirectional Data Flow Goals
  4. @nikolasburk State Management & Unidirectional Data Flow An Application is

    more than the Happy Path “In the context of software or information modeling, a happy path is a default scenario featuring no exceptional or error conditions.” Wikipedia
  5. The application state represents the information that is kept in

    memory of a running application and is the basis for the generation of a user interface. Application State? @nikolasburk State Management & Unidirectional Data Flow
  6. The application state represents the information that is kept in

    memory of a running application and is the basis for the generation of a user interface. Application State? @nikolasburk State Management & Unidirectional Data Flow
  7. The application state represents the information that is kept in

    memory of a running application and is the basis for the generation of a user interface. Application State? @nikolasburk State Management & Unidirectional Data Flow
  8. @nikolasburk State Management & Unidirectional Data Flow The main responsibility

    of an app is to transform an application state into a user interface. (AppState) -> UI
  9. Example: Conference Planner App @nikolasburk State Management & Unidirectional Data

    Flow struct Conference { let name: String let city: String let year: String var attending: Bool }
  10. Application State? @nikolasburk State Management & Unidirectional Data Flow conferences

    = [ Conference( name: "AppDevcon", city: "Amsterdam", year: "2017", attending: true ), Conference( name: "UIKonf", city: "Berlin", year: "2017", attending: false ), Conference( name: "WWDC", city: "San Jose", year: "2017", attending: true ) ]
  11. Application State? @nikolasburk State Management & Unidirectional Data Flow conferences

    = [ Conference( name: "AppDevcon", city: "Amsterdam", year: "2017", attending: true ), Conference( name: "UIKonf", city: "Berlin", year: "2017", attending: false ), Conference( name: "WWDC", city: "San Jose", year: "2017", attending: true ) ] selectedIndex = 0
  12. Application State? @nikolasburk State Management & Unidirectional Data Flow conferences

    = [ Conference( name: "AppDevcon", city: "Amsterdam", year: "2017", attending: true ), Conference( name: "UIKonf", city: "Berlin", year: "2017", attending: false ), Conference( name: "WWDC", city: "San Jose", year: "2017", attending: true ) ] selectedIndex = nil
  13. @nikolasburk State Management & Unidirectional Data Flow 1. Where should

    the state live? 2. Who is allowed to change the state? 3. How does the state get changed? 3 Golden Questions of State Management An architectural pattern should be able to answer these questions!
  14. @nikolasburk State Management & Unidirectional Data Flow How does the

    state get changed? KVO & Property Observers Delegates Callbacks NSNotificationCenter Target-Action 3
  15. @nikolasburk State Management & Unidirectional Data Flow UIViewController What’s wrong

    with MVC? Persistence Networking Navigation Domain Logic User Interface Concurrency
  16. @nikolasburk State Management & Unidirectional Data Flow • 3 Principles

    • Single source of truth • State is read-only • Changes with pure functions http://redux.js.org/docs/introduction/ThreePrinciples.html
  17. @nikolasburk State Management & Unidirectional Data Flow Main Concepts Store:

    … manages the application state … informs subscribers about changes Actions: … describe an intent to change the state Reducer: … pure functions that generate a new application state based on actions
  18. Application State (Old) AppDevcon UIKonf WWDC Reducer add Swift Summit

    to list and return new application state Action “Swift Summit” “San Francisco” “2017” Application State (New) AppDevcon UIKonf WWDC Swift Summit
  19. @nikolasburk State Management & Unidirectional Data Flow Helpful Resources •

    Unidirectional Data Flow in Swift: An Alternative to Massive View Controllers (Video) (https://realm.io/news/benji-encz- unidirectional-data-flow-swift/) • Real World Flux Architecture on iOS (http://blog.benjamin- encz.de/post/real-world-flux-ios/) • Redux Documentation (http://redux.js.org/) • Advanced iOS Application Architecture and Patterns, Andy Matuschak WWDC 2014 (Video) (http://asciiwwdc.com/2014/ sessions/229)