Calling RXJava2 Completable in a cascade - android

Usually to implement cascading tasks we use flatMap .
Like ObserableA.flatMap(A->ObserableB)
However I have a situation in which I would want to have two Completables that are interdependent.
Completable OperationA;
Completable OperationB;
I need OperationB to only execute after Operation A. Since there are no flatmap operators in Completable , how should I go about the situation?

You can use the andThen operator from the first Completable.
http://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Completable.html#andThen-io.reactivex.CompletableSource-

Related

android app Using Flow/coroutine on top of Rxjava2

I want to use coroutine in my kotlin app for must of it. but this app depends a lot on a service which return rxjava2 type object (so our input will be all the rxjava type). How can we still use coroutine in this model app instead of rxjava2. should we create a layer only convert rxjava object to normal object (or coroutine object?). or can we use both two together like:
SomeObservable
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe { value->
viewmodelScope.launch{
// send a flow or coroutine suspend fonction with value
}
},
Thanks,
You could wrap the RxJava functions with suspend functions.
See https://github.com/Kotlin/kotlinx.coroutines/issues/869
I want to answer my own question, It's not a good approche to use flow and rx at the same time. because it resolves the same problem. Rxjava + livedata is a clean architecture to go. or just use flow. so for resume:
Rxjava + livedata (a little bit coroutine to do some general
operation)
flow + coroutine
Two clean ways to make a good architecture. so if I have already rx type there is no need to convert to flow and reuse.

RxJava chaine of consecutive operations

I am a newbie in RxJava. I want to combine three consecutive asynchronous operations in to chain by RxJava2:
fun getDataFromRESTApi(): Observable<TheData>
saveDataToCache(theData: TheData): Completable
getDataFromCache(): Observable<TheData>
How can I do this? What rxjava methods I could to use?
I'm not sure what you trying to achieve exactly but there's the concatMap operator :
getDataFromRESTApi()
.concatMap(theData -> saveDataToCache(theData).toObservable())
.concatMap(cachedData -> getDataFromCache()) //maybe not needed
In the other hand if saveDataToCache returns the saved data (cached) you don't need the third line.

RxJava Subject and Completable

I have situation in which I try to subscribe to Completable that updates my DB. I cannot control when that method is triggered but I need to know if it has ended before I try to read data from it. I was thinking to make a Hot Completable and subscribe to it and check if it has completed. Now, I don't really know if I can subscribe with a Subject to a Completable. Should I go with Observable instead?
fun myCompletable(): Completable {...}
myCompletable.subscribe(mySubject)
mySubject.subscribe(checkIfCompletableHasCompleteMethod())
Is there another way to check if my method has completed before invoking the next one in an RX chain?
myRepo.someMethodThatInvokesMyCompletableMethodInternally()
.waitUntilMyCompletableMethodCompletes()
.subscribe(...)

RxJava: Merge an observable with a completable based on the result of the first emission of the observable

I have an observable that continually emits but I need to run a completable on a condition based on the first emission of the observable. The observable will continue to emit even when the completable has completed. I have tried looking around for an operator that does this, but I can't find one. How can I possibly achieve this?
Something like that -
observable
.flatMap(value -> {
return (value == something ? completableThatDoesSomething : Completable.complete())
.andThen(Observable.just(value));
})

Combine synchronous and asynchronous observables

Say that I have 5 observables, all of them are retrofit api call (one is for login)
Now i want to login, after that run all 4 other observables at the same time when the login progress has been finished.
Is there anyway I can do it?
Here code sample using rx on Kotlin
login()//need return Observable
.flatMap{ result->
//maby init calls observables
zip(firstCallObservable, secondObservable, thirdCallObservanle,fouthCallObservable){
first, second, third, fouth->
//do something with data
}}
.subscribeOn(Schedulers.io())
.observOn(AndroidSchedulers.mainThread())
.subscribe()

Categories

Resources