1

I have two observables that are chained together in a flatMap like this:

 override fun getProductObservable(): Observable<List<ProductModel>> {

return observable1(productId)
               .flatMap({
                 val limit = 8 - it.size
                   observable2(productId,  limit)
               }, { p1, p2 ->
                   p1.addAll(p2);  p1 //simply return observables1's results already packed with p2 results
               })

notice how observable2 depends on a result from observable1 (limit argument). BOTH OBSERVABLES RETURN THE SAME TYPE

Now onto my question, i would like observable1 once complete to immediately call onNext of the subscriber but still carry on to the flatmap. Essentially, the subscribers onNext would be called twice is what i want; once when observable1 completes and then again when the entire call completes. How can this be arranged ?

j2emanue
  • 60,549
  • 65
  • 286
  • 456

2 Answers2

0

Publish it and concatenate in the last value!

observable1(productId)
.publish( { shared ->
     Observable.concatArrayEager(
         shared.flatMap({
             val limit = 8 - it.size
               observable2(productId,  limit)
           }, { p1, p2 ->
               p1.addAll(p2);
           }),
         shared.takeLast(1)
     )
})
akarnokd
  • 69,132
  • 14
  • 157
  • 192
  • i could not make it work with concatEager but i used concat instead. but when i put a print statement in onNext it only fired once. – j2emanue Nov 06 '18 at 09:31
  • Updated the answer, use `concatArrayEager`. – akarnokd Nov 06 '18 at 09:49
  • what i see happening now is that in the subscriber its printing the last results twice. so essentially its returning the final result twice. i want it to return observable1 results in onNext and then afterwards return the final results. have you executed this ? maybe im missing something and thanks for showing me about concatArrayEager – j2emanue Nov 06 '18 at 10:56
  • How could I have executed it without knowing the definition of `observable1` and `observable2`, and possibly setting up the whole infrastructure and datasources behind them? Also your question said "the subscribers onNext would be called **twice**"; I interpreted this as you want the last item of `observable1` appear in the end output. – akarnokd Nov 06 '18 at 11:45
  • i put in bold that the observables have the same type. sorry for the misunderstanding – j2emanue Nov 06 '18 at 15:59
0

i could not believe how easy it was. i used startWith and it emitted the results of the first observable. AFter a second the final results came through. so it looks like this:

override fun getProductObservable(): Observable<List<ProductModel>> {

return observable1(productId)
               .flatMap({
                 val limit = 8 - it.size
                   observable2(productId,  limit)
                    .startWith(Observable.just(emptyList<ProductModel>()))
               }, { p1, p2 ->
                   p1.addAll(p2);  p1 //simply return observables1's results already packed with p2 results
               })
j2emanue
  • 60,549
  • 65
  • 286
  • 456