architecture-components-samples icon indicating copy to clipboard operation
architecture-components-samples copied to clipboard

why to use rxjava in new android architecture component

Open prabinshrestha opened this issue 6 years ago • 5 comments

  • what to the best way to use it?
  • when should we use it?
  • Do we need to use all live data?
  • Can we use as hybrid, live data + rxjava
  • Do we need to declare global variable every time we use LiveData or can we pass live data in method of some viewmodel?
  • Will android have all support for it in future as well

prabinshrestha avatar Sep 18 '17 12:09 prabinshrestha

@prabinshrestha

  • When your Businesses code (Repository Pattern) become Complex (maybe you have multiple threading operations)
  • Same as First Question
  • Start first using only Live Data (even in Repository Pattern) and then if Businesses code (Repository Pattern) become Complex start using Rx Java only in Repository Pattern part of App.
  • Yes it's recommendations from Google ( use live data in View --> ViewModel communication) and Rx Java (ViewModel ---> Businesses code (Repository Pattern) )
  • This is more general Java Question about reference variable
  • Yes it will

goransipic avatar Sep 23 '17 07:09 goransipic

Thank you so much from bottom of my heart.I will be very thankful if you provide me good sample example for it(using both),if possible?

*Yes it's recommendations from Google ( use live data in View --> ViewModel communication) and Rx Java (ViewModel ---> Businesses code (Repository Pattern) )

  • a little bit confused that you said we put our business logic and rxJava only in repository but the last point suggestion is to use rx from viewmodel to repository??

Again Thank you so much ;)

prabinshrestha avatar Sep 24 '17 04:09 prabinshrestha

Its recommended since Livedata is Lifecycle-aware. Rx isn't aslong as you dont dipose it with your lifecycle.

If you forget to unsubscribe/dispose your subscription if the view is gone you will get a leak. I dont like it to mix LiveData with RxJava this is why i have a composite disposable which is disposed in onCleared() / ViewModel or onStop / View.

mynote avatar Sep 28 '17 10:09 mynote

@mynote

The threading is ultimate easy with RxJava so I am using it, if there is any easiest option available with LiveData i will use it.Thanks

prabinshrestha avatar Oct 04 '17 06:10 prabinshrestha

@prabinshrestha , there are several ways solving that.

Flowable.just(yourDao.updateQuery())
Flowable.create( e -> e.onNext(yourDao.updateQuery()))
Flowable.fromCallable( () -> yourDao.updateQuery()) 

Better use Single, since you can only receive one result for Update/Delete/Insert. No need to implement it into Room by default.

mynote avatar Oct 04 '17 09:10 mynote