0

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

estn
  • 1,203
  • 1
  • 12
  • 25
  • what does `fetchFromDb()` returns? If it's not `Single` then it wouldn't work. Concat operator works like: emit all from first do it till it's not `onComplete` and only after that it starts to emit items from the second one. – borichellow Feb 01 '19 at 13:57
  • both fetchFromDb and fetchFromApi are Single and its concatArrayEager operator – estn Feb 01 '19 at 13:58
  • Then i guess problem is in `.firstOrError()` It make your Flowable to Single. From it docs: `Returns a Single that emits only the very first item emitted by this Flowable or signals a NoSuchElementException if this Flowable is empty.` So it emits first one (which is DB data) – borichellow Feb 01 '19 at 14:01
  • Yes I want that to be single, thats not an issue. My problem is that api Single is not started - I dont see any api call. I dont need result of that api call, I just want to fire it off and if its successful, update the database with it – estn Feb 01 '19 at 14:06
  • you can use Observable.concat(fetchFromApi(),fetchFromDb()).it will wait for both observable to complete and then return result. – Deep Naik Feb 01 '19 at 14:08
  • Guys thanks for you help but I am wondering why concatArrayEager didnt subscribe to all singles, you are not answering my question – estn Feb 01 '19 at 14:14

1 Answers1

1

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.

tynn
  • 38,113
  • 8
  • 108
  • 143
  • Makes sense now, I see that I basically added race condition with this design, thanks – estn Feb 02 '19 at 14:17