Slide 1

Slide 1 text

RxSwift & MVVM Your first steps

Slide 2

Slide 2 text

About Me iOS Tech Lead @ Gett Open Source ❤ Hackathon fan and winner of some International Speaker @freak4pc

Slide 3

Slide 3 text

About Me Involved in dozens of tutorials and several published books Author & Editor at raywenderlich.com iOS Team @freak4pc

Slide 4

Slide 4 text

So, what are we going to talk about today?

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

Architecture Create order, unification and simplification when building software systems

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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.

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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.

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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*

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

RxSwift Observable Streams

Slide 17

Slide 17 text

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 BehaviorRelay PublishSubject BehaviorSubject Observable Driver Signal View subscribes to: RxSwift Observable Streams

Slide 18

Slide 18 text

Start with a plan What are my inputs? What are my outputs?

Slide 19

Slide 19 text

Start with a plan What are my inputs? email password tappedLogin What are my outputs? isLoginEnabled loginButtonTitle loginMessage

Slide 20

Slide 20 text

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 is button enabled Driver typed email PublishRelay PublishSubject typed password PublishRelay PublishSubject

Slide 21

Slide 21 text

against my better judgement…

Slide 22

Slide 22 text

TIME FOR A LIVE DEMO

Slide 23

Slide 23 text

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.

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

Shai Mishali freak4pc Thank you!