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

MVP & RxJava

MVP & RxJava

Collaborative talk with Niklas Baudy (https://twitter.com/vanniktech)

We'll talk about the upcoming release of RxJava 2 and how to use MVP in a reactive manner.
What we'll talk about:
- RxJava 1 (History, basic introduction)
- RxJava 2 (Reason / Motivation, What's new / What stayed)
- MVP with RxJava 2 + RxBinding

Serj Lotutovici

October 15, 2016
Tweet

More Decks by Serj Lotutovici

Other Decks in Programming

Transcript

  1. RxJava 1.x - Backpressure • Observable “mostly” supports it ◦

    onBackPressureXXX() • Single & Completable do not support Backpressure
  2. RxJava 2.0 - Timeline • Still under development • 2.0.0-RC1

    (25th August 2016) • 2.0.0-RC2 (5th September 2016) • 2.0.0-RC3 (23rd September 2016) • 2.0.0-RC4 (7th October 2016) • 2.0.0-RC5 (21st October 2016) • 2.0.0 (28th October 2016)
  3. RxJava 2.0 - Reasons • Reactive Streams Specification • New

    breaking API • Avoid / smoothen out some mistakes • Less allocations / overhead
  4. RxJava 2.0 - What changed? • Different package name •

    Different Artifact Id • null is forbidden • New Reactive Base Types (Flowable & Maybe) • Subscription management • Better conversion and integration between Reactive Base Types
  5. RxJava 2.0 - null is forbidden • Basically no nulls

    are allowed • Observable.just(null) • Observable.just(1, null) • Single.just(null) • Single.error(null) • Single.fromCallable(() -> null)
  6. RxJava 2.0 - Reactive Base Types • Observable<T> • Single<T>

    • Completable • Flowable<T> • Maybe<T>
  7. RxJava 2.0 - Subscribing • Observable: void subscribe(Observer<T>) • Flowable:

    void subscribe(Subscriber<T>) • subscribeWith() • No safe subscribe • Lambdas support too
  8. RxJava 2.0 - Better Integration • Single<T> first(default) • Maybe<T>

    firstElement() • Single<T> firstOrError() • Maybe<T> last() • Maybe<T> elementAt(index)
  9. RxJava 2.0 - Early Adopters • RxAndroid • https://github.com/ReactiveX/RxAndroid •

    Retrofit Adapter • https://github.com/JakeWharton/retrofit2-rxjava2-adapter
  10. RxJava 2.0 <-> RxJava 1.x • Adapter by the Lead

    RxJava Developer (David) • https://github.com/akarnokd/RxJava2Interop
  11. MVP

  12. MVP::motivation • Android Platform Types are untestable • Separation of

    UI & Business Logic • Forces structure and layering
  13. MVP::in a nutshell • Presenter - contains the business logic

    ◦ Acts upon the Model & the View ◦ Responsible for retrieving data (the Model) ◦ Responsible for formatting the data for display (in the View) • View - interface/contract that displays data ◦ ≠ android.view.View ◦ Routes user events to the Presenter ◦ Usually implemented by Activity, Fragment or ViewGroup • Model - defines the data that needs to be displayed
  14. RxBinding • RxJava apis for Android UI widgets • Bindings

    for platform views, as well as for support library views • Provides Kotlin extension functions • No RxJava 2.0 adaptation at the moment • Not safe with RxJava2Interop
  15. RxBinding::motivation • View events == Stream of Data • Seamless

    integration into Reactive sequences • Observable creation requires a great deal of ceremony ◦ Especially without leaking the view
  16. Reactive MVP • Same principle as Imperative MVP • With

    two major differences: ◦ The view propagates UI events as Reactive Streams ◦ The presenter exposes only one method which returns a Disposable
  17. Reactive MVP::takeaways • User input may also create Backpressure ◦

    Consider Flowable or Observable#filter()/Observable#debounce() • Keep your presenters simple • A ‘View’ is not always an actual UI representation https://github.com/serj-lotutovici/RxMVP