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

Subscribe RxJava vs LiveData English

Subscribe RxJava vs LiveData English

2018 edition, in English

Bruno Aybar

October 20, 2018
Tweet

More Decks by Bruno Aybar

Other Decks in Programming

Transcript

  1. • Allow us to observe changes over some kind of

    data, and to react to those changes. • Propose a reactive paradigm • Do they do the same? RxJava & LiveData
  2. OBSERVER pattern Design pattern that defines a dependency between objects

    (…), in which one of them changes its state, it notifies about it to all of its dependants
  3. RxJava An API for asynchronous programming
 with observable streams Is

    a combination of the best ideas from
 the Observer pattern, the Iterator pattern, and functional programming ReactiveX RxJava RxSwift RxJS Rx.NET RxPY RxPHP …
  4. package org.reactivestreams; public interface Subscriber<T> {
 
 public void onNext(T

    t);
 
 public void onError(Throwable t);
 
 public void onComplete();
 }

  5. Flowable.just(1).subscribe(
 { value -> log("onNext: $value") },
 { error ->

    log("onError: $error")},
 { log("onComplete!") }
 )
  6. Flowable.just(1).subscribe(
 { value -> log("onNext: $value") },
 { error ->

    log("onError: $error")},
 { log("onComplete!") }
 )
  7. val subscription = Flowable.just(1).subscribe(
 { value -> log("onNext: $value") },


    { error -> log("onError: $error")},
 { log("onComplete!") }
 ) subscription.dispose()
  8. Flowable.just(1).subscribe(
 { value -> log("onNext: $value") },
 { error ->

    log("onError: $error")},
 { log("onComplete!") }
 )
  9. Flowable.just(1)
 .subscribeOn(Schedulers.computation()) .subscribe(
 { value -> log("onNext: $value") },
 {

    error -> log("onError: $error")},
 { log("onComplete!") }
 ) Thread in which the operation
 will be performed
  10. Flowable.just(1)
 .subscribeOn(Schedulers.computation())
 .observeOn(AndroidSchedulers.mainThread()) .subscribe(
 { value -> log("onNext: $value") },


    { error -> log("onError: $error")},
 { log("onComplete!") }
 ) Thread in which we will listen 
 to the stream emissions
  11. Flowable.fromArray(1,2,3,4)
 .filter { it % 2 == 0 }
 .map

    { it + 10 } Flowable.just(User(“1”, “Bruno”))
 .map { it.name }
  12. Combine CombineLatest And/Then/When Zip Switch Join ... Filtering Filter Distinct

    First Last Take ... Transform Map FlatMap Scan GroupBy Buffer ... More: http:/ /reactivex.io/documentation/ operators.html
  13. Flowable.fromArray(1,2,3) 1 2 3 MutableLiveData<Integer> data = 
 new MutableLiveData<>();


    data.setValue(1); //UI thread
 data.setValue(2); //UI thread
 data.postValue(3); //Background thread
  14. Flowable.fromArray(1,2,3) 1 2 3 MutableLiveData<Integer> data = 
 new MutableLiveData<>();


    data.setValue(1); //UI thread
 data.setValue(2); //UI thread
 data.postValue(3); //Background thread
  15. LifecycleOwner class that contains information about a component lifecycle, and

    allow other objects to observe its changes Destroyed Created Started Resumed Initialized
  16. LifecycleObserver public class MyObserver implements LifecycleObserver { 
 @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
 public

    void connectListener() {
 
 }
 
 @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
 public void disconnectListener() {
 
 }
 }

  17. Flowable
 <Int> Activity 1 2 3 subscribes to data.subscribe {

    value -> textView.setText("Text " + value) }
  18. MutableLiveData<Integer> data = 
 new MutableLiveData<>();
 data.setValue(1); //UI thread
 data.postValue(2);

    //Background thread data.observe(lifecycleOwner, { value -> // executes in UI thread })
  19. val userLiveData: LiveData<User> = … 
 val userNameLiveData = Transformations.map(

    userLiveData, { it.name }) Map SwitchMap Custom Transformations
  20. RxJava vs LiveData Explicit code ✔ Thread handling ✔ Powerful

    Operators ✔ Steep learning curve ✘ Really simple ✔ Android-oriented ✔ Integration with AC ✔ Not so powerful
 operators ✘ Library compatibility 
 (i.e. Retrofit) ✔ Over-engineered? ✘ Portable knowledge ✔
  21. Additional Material Intro to RxJava (Christina Lee) https:/ /www.youtube.com/watch?v=XLH2v9deew0 Learning

    Rx (for Android) by Example https:/ /www.youtube.com/watch?v=k3D0cWyNno4 Common RxJava Mistakes https:/ /www.youtube.com/watch?v=QdmkXL7XikQ RxJava in Baby Steps https:/ /www.youtube.com/watch?v=YPf6AYDaYf8 RxMarbles http:/ /rxmarbles.com/
  22. Live Data docs https:/ /developer.android.com/topic/libraries/architecture/livedata.html LiveData & Lifecycle https:/ /www.youtube.com/watch?v=jCw5ib0r9wg

    ViewModels, LiveData and Lifecycles, oh my! https:/ /www.youtube.com/watch?v=SlZVYkhoSq8 Android lifecycle-aware components codelab https:/ /codelabs.developers.google.com/codelabs/android-lifecycles Additional Material
  23. @brunoaybarg @bruno.aybar Bruno125 Bruno Aybar Android Dev Perú Organizer Android

    Engineer @ Avantica Gracias! https://speakerdeck.com/bruno125/subscribe-rxjava-vs-livedata-2018