I’ve recently been playing with flow and architecture components. While on it, I’ve been pondering how to implement the following data flow. I could use some tips on how to improve my current implementation. My use case is that I’m using TMDB API with two endpoints: one to fetch a list of genres and second to fetch a list of movies per genre. This means that if the API returns N genres I have to make N requests to fetch the list of movies. My repository returns a flow of these objects, for example Flow<List<Genre> and Flow<List<Movie>>.
My question is, what would be a
flow way of “combining” these two streams, so that once the genres stream emits a value (it will only emit one value, all the genres), the genres would be looped over and movies would be queried for each, then those two combined.
Currently I’m storing the Genres from first request into a LiveData, then using
switchMap
on that to create another LiveData,
GenresWithMovies
. Inside the switchMap I invoke my UseCase in which I have some 🍝 to query Movies for each of the genres.
If I were to do this without flow I’d do something like:
fun fetchData() {
val genresWithMovies: List<GenreWithMovies> = dataSource.queryGenres().map { genre ->
val movies = dataSource2.queryMovies(genre.id)
genre.movies = movies
genre
}
}
(crossposted from
#flow )