Search code examples
kotlinkotlin-coroutineskotlin-flow

How to transform Flow<List<Flow<T>>> to Flow<List<T>>


I have a database query that returns the list of indexes of objects for a certain day, i.e.:

getIndexesOfDay(day: Day): Flow<List<String>>

Now, I need to listen for details for each index, for instance:

getDetailsByIndex(index: String): Flow<Details>

So the question is: how can I accomplish this using Flows?

Two important constraints:

  1. Changes on the indexes should propagate to the final flow;
  2. Changes on any Details object should also update the flow.

What I have already tried:

getIndexesOfDay(todayDay)
          .map { indexes ->
              indexes?.map {
                      mIndex->
                  getDetailsByIndex(mIndex)
              }
          }

But it returns a Flow<List<Flow<Details>>> instead of the desired Flow<List<Details>>

Appreciate any help!


Solution

  • I haven't tested this, but I believe it should be possible using flatMapLatest() and combine():

    getIndexesOfDay(todayDay)
        .flatMapLatest { indexes ->
            combine(indexes.map { getDetailsByIndex(it) }) { it.asList() }
        }
    

    For each new list of indexes, it invokes getDetailsByIndex() multiple times to acquire a list of flows and then combines these flows into a single flow of details lists.

    The drawback is that whenever indexes change, it throws away all Flow<Details> objects, even if an index existed in both old and new list of indexes. It would be probably better to create some kind of a cache of detail flows per each index.