1

I'm trying to combine multiple observables into one with the following code:

 val observables: Array<Observable<out MyItem>> = arrayOf(
    obs0,
    obs1
    obs2,
    obs3,
    obs4.flatMap { Observable.fromIterable(it) },
    obs5,
    obs6,
    obs7,
    obs8,
    obs9,
    obs10
)

Observable.combineLatestDelayError(observables) { items ->
    items.filterIsInstance<MyItem>()
        .filter { ... }
        .map { ... }
}

All of them has a type of Observable<MyItem>, except obs4 is Observable<List<MyItem>>, so I want to unpack it in the result list with .flatMap { Observable.fromIterable(it) }. The problem here is if obs4 emits an empty list then the resulting chain never emits, because combineLatest waits for all sources to emit first item. Is there any operator that would emit on every change of every source ignoring empty sources?

akarnokd
  • 69,132
  • 14
  • 157
  • 192
Ilya E
  • 700
  • 7
  • 13
  • How many items do you expect from each `obs0`...`obs10`? Does it matter which `MyItem` came from which `obs`? Perhaps you can use `merge`, `toList` and `map` instead of `combineLatest`. – akarnokd Nov 09 '21 at 13:24
  • There can be from 1 up to 4 items in each `obs`, but I don't know the exact number, and yes order matters. I guess `merge`+`toList` is not an option, since I need multiple emissions not a `Single`. I might end up using `Any` as an Array type parameter and unsafe casting in combiner to create a list. – Ilya E Nov 09 '21 at 16:00
  • 2
    You could introduce a placeholder `MyItem` and use `defaultIfEmpty` with `fromIterable` so you do have a value from obs4. – akarnokd Nov 09 '21 at 16:10
  • Thanks for the idea! Fortunately, I can add stub `MyItem` implementation – Ilya E Nov 09 '21 at 16:43

0 Answers0