I am writing some repository logic for android application, I have this logic for retrieving data from database and updating the database with data from api immediately after that.
fun fetchData(): Single<Data> {
return Single.concatArrayEager(
fetchFromDb(),
fetchFromApi())
.firstOrError()
}
By the way - database is Room and api is Retrofit. Now what happens is that room emits almost instantly, but for some reason api request is not fired - the second Single
doesn't start.
I read the documentation on the eager operator and I am not sure if this is correct behavior. I even tried to delay the database fetch, by like 20 miliseconds - which resulted in that second single being actually fired
The concatArrayEager
should subscribe to all Observables
. But your API call might take some time to actually start. This might be more time than your DB call needs to return.
Now we see the effect of firstOrError
. When the first value is received, the stream is terminated and all active or pending subscriptions of the concatenation are canceled. Also the API result won't ever be used.