android / architecture-components-samples

Samples for Android Architecture Components.
https://d.android.com/arch
Apache License 2.0
23.4k stars 8.29k forks source link

why to use rxjava in new android architecture component #157

Open prabinshrestha opened 6 years ago

prabinshrestha commented 6 years ago
goransipic commented 6 years ago

@prabinshrestha

prabinshrestha commented 6 years ago

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) )

Again Thank you so much ;)

mynote commented 6 years ago

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.

prabinshrestha commented 6 years ago

@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

mynote commented 6 years ago

@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.