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

Live Data vs Observables in Android

Live Data vs Observables in Android

MobiTalk Chennai - Mobile Developers Meetup

Youtube Link:
https://youtu.be/DEjgNQ6lOAc

Discord Community (Join us here for latest updates):
http://bit.ly/mobitalkchat

Topic:
Live Data vs Observables in Android

About the Event:
MobiTalk is a Mobile Meetup focused on both Android and iOS developers. This meetup is organised currently in Chennai.

Join us for the first session of MobiTalk meetup where you can learn and explore things related mobile app development.

Date of Event:
Sat, May 25, 2019 10:30 AM – 12:30 PM IST

About the Presenter:
Jyoti Dubey (@its_jaede) is working as a Product Developer @ FreshWorks.

Avatar for MobiTalk

MobiTalk

May 25, 2019

More Decks by MobiTalk

Other Decks in Programming

Transcript

  1. Lifecycle And Config changes • Fetch something over the network

    • Show loading status • Display the result
  2. Lifecycle And Config changes • Fetch something over the network

    • Show loading status • Display the result
  3. Why? • UI State cannot be changed after onSaveInstanceState() •

    Fragment transaction are UI States • Applies to views
  4. private void load() { compositeDisposable.add(repository.fetchUsers() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(user -> isApiFetchSuccessfulSubject.onNext((!user.name.isEmpty())));

    } } private void load() { compositeDisposable.add(repository.fetchUsers() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(user -> isApiFetchSuccessfulLiveData.setvalue(!user.name.isEmpty()))); } }
  5. Observable vs Livedata subscription Comparison private void observeForDataChanges() { viewModel.isApiFetchSuccessfulLiveData.observe(this,result->{

    if(result){ ApiFetchSuccessDialog dialog = new ApiFetchSuccessDialog(); dialog.show(getFragmentManager(), ApiFetchSuccessDialog.TAG); } }); } private void observeForDataChanges() { compositeDisposable.add(viewModel.isApiFetchSuccessfulSubj ect.subscribe(result->{ if(result){ ApiFetchSuccessDialog dialog = new ApiFetchSuccessDialog(); dialog.show(getFragmentManager(), ApiFetchSuccessDialog.TAG); } })); } @Override protected void onDestroy() { compositeDisposable.dispose(); super.onDestroy(); }
  6. LIVE DATA BEHAVIORSUBJECT ‣ Caches at most one item ‣

    Re-emit last known item ‣ Emission always happens on main thread ‣ Emits only when life cycle owner is in safe state ‣ Disposes automatically when the lifecycle owner is destroyed ‣ No error channel ‣ Less transformation operators ‣ Caches at most one item ‣ Re-emit last known item ‣ Can be observed on any thread ‣ Emits irrespective of life cycle ‣ Has to be disposed manually ‣ Has error channel ‣ Abundance of transformation operators
  7. LIVE DATA BEHAVIORSUBJECT ‣ Caches at most one item ‣

    Re-emit last known item ‣ Emission always happens on main thread ‣ Emits only when life cycle owner is in safe state ‣ Disposes automatically when the lifecycle owner is destroyed ‣ No error channel ‣ Less transformation operators ‣ Caches at most one item ‣ Re-emit last known item ‣ Can be observed on any thread ‣ Emits irrespective of life cycle ‣ Has to be disposed manually ‣ Has error channel ‣ Abundance of transformation operators
  8. Drawbacks • Live data transformations happen on main thread •

    Operators supported are: map(), flatmap(), distinctUntilChanged()
  9. • Live data should appear in last layer of UI

    Update • Rx excels in transformation operators and multi-threading. • Choose Rx if user is not going to touch the UI much • Work by combining observable and live data. Conclusion