Reputation: 463
I want to invoke one Completable after another. That is when first is finished, invoke second and when the second is finished, invoke the third one. I already tried some different stuff like andThen(), but found out it does not "wait" for previous Completable to finish as it runs parallel. So far, this is what I have found and it works as I wanna, but is there any better way to improve this. Is there any operation function or something to get rid of Completable.defer at every stage?
Here is so far working example:
private fun invokeAllThreeDoSomeLogic() {
disposable.add(Completable.concatArray(
Completable.defer {
firstApi.getData().doOnError { t: Throwable? ->
Timber.w(
t,
"first error"
)
}
},
Completable.defer {
Completable.fromObservable(secondApi.getData()
.doOnError { t: Throwable? ->
Timber.i(
t,
"Second error"
)
}
},
Completable.defer {
thirdApi.refresh().doOnError { t: Throwable? ->
Timber.i(
t,
"Third error"
)
}
}
).subscribe(
{ },
{ t: Throwable? ->
Timber.w(t, "something went wrong")
})
)
}
Upvotes: 0
Views: 500
Reputation: 6107
Use FlatMap for chained api call. It will be like
firstApi.getData()
.flatmap(dataFromApi1 -> return secondApi.getData())
......
......
Check this medium blog for better understanding.
Edit:
For Completable you can use andThen
operator .
firstCompletable
.andThen(secondCompletable)
Please check this answer.
From that answer:
In general, this operator is a "replacement" for a flatMap on Completable:
Upvotes: 0