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.

Shai Mishali

January 30, 2019
Tweet

More Decks by Shai Mishali

Other Decks in Programming

Transcript

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

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

    books Author & Editor at raywenderlich.com iOS Team @freak4pc
  3. 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
  4. 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.
  5. 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
  6. 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.
  7. 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
  8. 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*
  9. 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
  10. 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
  11. 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
  12. Start with a plan What are my inputs? email password

    tappedLogin What are my outputs? isLoginEnabled loginButtonTitle loginMessage
  13. 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>
  14. 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.
  15. 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