Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

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

I have 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!

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!