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

RxSwift & MVVM: Your First Steps

RxSwift & MVVM: Your First Steps

Talk Video: https://www.youtube.com/watch?v=6KXqa4iLCUM

Come and learn how to get started with RxSwift and MVVM. What is really an architecture, and what MVVM aims to solve over the standard usage of MVC? Why is RxSwift such a wonderful companion for employing MVVM?

This presentation also includes actual live coding for building a basic MVVM-based login screen.

8477f7be4418a0ce325b2b41e5298e4c?s=128

Shai Mishali

January 30, 2019
Tweet

More Decks by Shai Mishali

Other Decks in Programming

Transcript

  1. RxSwift & MVVM Your first steps

  2. About Me iOS Tech Lead @ Gett Open Source ❤

    Hackathon fan and winner of some International Speaker @freak4pc
  3. About Me Involved in dozens of tutorials and several published

    books Author & Editor at raywenderlich.com iOS Team @freak4pc
  4. So, what are we going to talk about today?

  5. Architecture (ärˈkĭ-tĕk-chər) “The art or practice of designing and constructing

    buildings” $
  6. Architecture (ärˈkĭ-tĕk-chər) “The art or practice of designing and constructing

    buildings” software
  7. Architecture Create order, unification and simplification when building software systems

  8. MVC Very common. The standard architecture recommended by Apple for

    building iOS apps since its early days. Model-View-Controller Model Storage, Networking, etc. View UI Only Controller Business Logic
  9. MVC Model-View-Controller Model Storage, Networking, etc. View UI Only Controller

    Business Logic UIViewController Highly Coupled Easily breaks SRP Not easily testable Easily achieves thousands of LoC
 MVC = Massive View Controller Very common. The standard architecture recommended by Apple for building iOS apps since its early days.
  10. MVVM Model-View-View Model Model Storage, Networking, etc. Higher separation of

    concerns. ViewModel processes UI events to give the UI what it needs to present, via the Model/Service layer. View(Controller) Layout and Presentation Logic ViewModel Business Logic - Prepares data for presentation Services UI Events Updates Request Response
  11. MVVM Model-View-View Model Model Storage, Networking, etc. View(Controller) Layout and

    Presentation Logic ViewModel Business Logic - Prepares data for presentation Services UI Events Updates Request Response MVC Issues MVVM Resolution Highly Coupled View highly separated from logic and data; Logic and UI can be reused Easily breaks SRP Each piece has a relatively single responsibility Not easily testable ViewModel is easily testable since it transforms inputs to outputs Easily achieves thousands of LoC Separation into files reduces chances of thousand-lines files Massive View Controller View Controller only performs layout, so it is quite light. ViewModel can be quite heavy but not as heavy as MVC Higher separation of concerns. ViewModel processes UI events to give the UI what it needs to present, via the Model/Service layer.
  12. MVVM Model-View-View Model Model Storage, Networking, etc. View(Controller) Layout and

    Presentation Logic ViewModel Business Logic - Prepares data for presentation Services UI Events Updates Request Response
  13. MVVM Model-View-View Model How do you notify the ViewModel of

    ViewController Events? • Swift’s didSet - good for imperative object/value changes • Specific methods when things change, and invoking those • If the changes are UIControlEvent(s), we also some of delegates/targets Model Storage, Networking, etc. View(Controller) Layout and Presentation Logic ViewModel Business Logic - Prepares data for presentation Services UI Events Updates Request Response /PUHSFBUTPMVUJPOT CVUXFDBONBLFUIFNXPSL*
  14. MVVM Model-View-View Model How do you propagate updates back to

    the view layer? • Delegates - you would need one per-output • Closures - same issue, plus potential retain cycles • Manual polling / KVO / Notification Center - Model Storage, Networking, etc. View(Controller) Layout and Presentation Logic ViewModel Business Logic - Prepares data for presentation Services UI Events Updates Request Response $BO`USFBMMZNBLFUIJTXPSLOPUTDBMBCMFDPNQPTBCMF
  15. MVVM Model-View-View Model How do you propagate updates back to

    the view layer? • Delegates - you would need one per-output • Closures - same issue, plus potential retain cycles • Manual polling / KVO / Notification Center - Model Storage, Networking, etc. View(Controller) Layout and Presentation Logic ViewModel Business Logic - Prepares data for presentation Services UI Events Updates Request Response $BO`USFBMMZNBLFUIJTXPSLOPUTDBMBCMFDPNQPTBCMF
  16. RxSwift Observable Streams

  17. View(Controller) Layout and Presentation Logic ViewModel Business Logic - Prepares

    data for presentation ViewController notifies ViewModel of Events ViewModel notifies View of updates View binds to: Relays & Subjects PublishRelay<T> BehaviorRelay<T> PublishSubject<T> BehaviorSubject<T> Observable<T> Driver<T> Signal<T> View subscribes to: RxSwift Observable Streams
  18. Start with a plan What are my inputs? What are

    my outputs?
  19. Start with a plan What are my inputs? email password

    tappedLogin What are my outputs? isLoginEnabled loginButtonTitle loginMessage
  20. View(Controller) Layout and Presentation Logic ViewModel Business Logic - Prepares

    data for presentation ViewController notifies ViewModel of Events ViewModel notifies View of updates RxSwift Observable Streams Observable<Bool> is button enabled Driver<Bool> typed email PublishRelay<String> PublishSubject<String> typed password PublishRelay<String> PublishSubject<String>
  21. against my better judgement…

  22. TIME FOR A LIVE DEMO

  23. In summary MVVM & RxSwift • View(Controller) is mainly responsible

    for presentation logic. • View Model encapsulates business logic for view (via additional services). • View Model is easily testable and reusable. • View itself also becomes a reusable entity, since its decoupled from specific logic. • RxSwift lets us easily leverage a unified notification & binding mechanism between the View and its View Model.
  24. Want to learn more? RxSwift-Specific ~50 minutes Hebrew @ Gett

    iOS Meetup 2018, Tel Aviv: https://youtu.be/I-npe1z6HbA English @ Swiftconf 2018, Germany: https://youtu.be/GdvLP0ZAhhc Rx for Mobile (Platform-agnostic) ~ 30 minutes English @ Mobile at Scale, Facebook: https://bit.ly/2QWZ91D
  25. Shai Mishali freak4pc Thank you!