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

MVVM & RxSwift

Avatar for Dang Thai Son Dang Thai Son
September 14, 2016

MVVM & RxSwift

MVVM & RxSwift

Avatar for Dang Thai Son

Dang Thai Son

September 14, 2016
Tweet

Other Decks in Programming

Transcript

  1. HELLO!!! Dang Thai Son • iOS Developer 2+ years experience

    • Interested in Functional Programming • Going to learn Elm/Haskell this year. • Blog: https://dangthaison91.wordpress.com https://dangthaison91.wordpress.com | fb.me/dangthaison.91 | @dangthaison91 2
  2. MVC Start project from scratch: • Project grow bigger with

    more features • Run for deadline => Do you try to update / add functions after 1-2 months later? https://dangthaison91.wordpress.com | fb.me/dangthaison.91 | @dangthaison91 11
  3. MVC Plain old MVC is leads to huge, disorganized, unmaintainable

    code (most of it in a UIViewController). https://dangthaison91.wordpress.com | fb.me/dangthaison.91 | @dangthaison91 13
  4. View Controllers • Responsible for controlling views (Layout, Animation, Colors,

    Presenting VC...) • Tell ViewModel when something happens (handles user interaction). • Listen to ViewModel (network request completes, data changed). https://dangthaison91.wordpress.com | fb.me/dangthaison.91 | @dangthaison91 15
  5. View Models • Perform all the business logic (fire-off network

    requests, fetch Core Data objects, validate input...) • Deliver necessary data to view controllers to display: Date/String format. • Update and listen to changes in models https://dangthaison91.wordpress.com | fb.me/dangthaison.91 | @dangthaison91 16
  6. Advantages 1. Maintainability • Separation of concerns • Redesign UI

    2. Testability • Easy to write unit test. • No require UI Automation 3. Extensibility: More reusable for iOS, watchOS, MacOS... https://dangthaison91.wordpress.com | fb.me/dangthaison.91 | @dangthaison91 18
  7. Disadvantages • Can be overkill for Simple UI: Tutorial/ Walkthrough

    • Massive View Controller will become Massive ViewModel • MVC is has better speed of the development. • Hard to design the ViewModel. https://dangthaison91.wordpress.com | fb.me/dangthaison.91 | @dangthaison91 19
  8. View Controllers • View only knows the view controllers, notifying

    them of interaction events (same as MVC). • View controller can't talk to models directly, they only interact with view models and views. https://dangthaison91.wordpress.com | fb.me/dangthaison.91 | @dangthaison91 21
  9. View Models • Don’t import UIKit. • View Model only

    knows models. • View Model doesn't know Views/View Controllers https://dangthaison91.wordpress.com | fb.me/dangthaison.91 | @dangthaison91 22
  10. MVVM is silver bullet and will save your life from

    MVC? https://dangthaison91.wordpress.com | fb.me/dangthaison.91 | @dangthaison91 23
  11. What is Functinal Reactive Programming? • FRP is about datatypes

    that represent a value ‘over time’ • FRP is programming with asynchronous data streams. • ... https://dangthaison91.wordpress.com | fb.me/dangthaison.91 | @dangthaison91 31
  12. What we can do • Observables • Transformations • Composition

    • Combination • Filter • Buffer ... https://dangthaison91.wordpress.com | fb.me/dangthaison.91 | @dangthaison91 34
  13. FRP Advantages • Code more concise & clear, easy to

    understand without context • Readability, highly express • Make Asynchronous easier • UI Binding • Maintainability, Extensibility ... https://dangthaison91.wordpress.com | fb.me/dangthaison.91 | @dangthaison91 36
  14. FRP Advantages • Readable declarative code: review, test, debug •

    Has a standard mechanism for error recovery. • Composing is awesome!!! https://dangthaison91.wordpress.com | fb.me/dangthaison.91 | @dangthaison91 38
  15. FRP Advantages • Handling UI interactions/events/state is insanely easy •

    Reduce side-effects & using mutable values. • Remove “callback hell” https://dangthaison91.wordpress.com | fb.me/dangthaison.91 | @dangthaison91 39
  16. FRP Advantages FRP helps us focus on solving the problem

    rather than the required procedure. https://dangthaison91.wordpress.com | fb.me/dangthaison.91 | @dangthaison91 40
  17. Best Practices View Model doesn't know about View Controller. =>

    So how it give know what information VC need? https://dangthaison91.wordpress.com | fb.me/dangthaison.91 | @dangthaison91 43
  18. MVVM rules with RxSwift View cannot modify the data. View

    only knows how to present the data. • Bad viewModel.title .map { $0 + "!!!" } // View should not modify the data .bindTo(self.titleLabel) • Good viewModel.title .bindTo(self.titleLabel) https://dangthaison91.wordpress.com | fb.me/dangthaison.91 | @dangthaison91 52
  19. MVVM rules with RxSwift View doesn't know what ViewModel does.

    View can only communicate to ViewModel about what View did. • Bad viewModel.login() // Bad: View should not know what ViewModel does (login) • Good self.loginButton.rx_tap .bindTo(viewModel.loginButtonDidTap) // "Hey I clicked the login button" https://dangthaison91.wordpress.com | fb.me/dangthaison.91 | @dangthaison91 53
  20. MVVM rules with RxSwift Model is hidden by ViewModel. ViewModel

    only exposes the minimum data so that View can render. • Bad struct ProductViewModel { let product: Driver<Product> // Bad: ViewModel should hide Model } • Good struct ProductViewModel { let productName: Driver<String> let formattedPrice: Driver<String> } https://dangthaison91.wordpress.com | fb.me/dangthaison.91 | @dangthaison91 54
  21. Tips • Handle error: parse Error Message in ViewModel then

    expose as String. => Oops, we are doing it wrong too. • Don't make ViewModelM become Presenter layer: number of rows, color... https://dangthaison91.wordpress.com | fb.me/dangthaison.91 | @dangthaison91 55
  22. There are only two hard things in Computer Science: Cache

    invalidation and Naming things. — Phil Karlton https://dangthaison91.wordpress.com | fb.me/dangthaison.91 | @dangthaison91 57
  23. Naming? • Relate to Data, Action, not related to UI

    • Input: Trigger, Source, Action... • Output: Signal, Sink... https://dangthaison91.wordpress.com | fb.me/dangthaison.91 | @dangthaison91 58
  24. Q&A Where MVVM suck: UI with complex state & levels.

    => Solution: • VIPER ! • Flux/Redux "" https://dangthaison91.wordpress.com | fb.me/dangthaison.91 | @dangthaison91 63
  25. Reactive with us, we are hiring! • PHP/Ruby Backend •

    Android Developer • iOS with Swift • Designer https://itviec.com/companies/innovatube https://dangthaison91.wordpress.com | fb.me/dangthaison.91 | @dangthaison91 64