Marko Novaković
06/18/2024, 8:53 PMdata class Comment(...)
data class Post(val comments: List<Comment>, ...)
both, posts and comments, are exposed as `Flow`s.
I want a flow that re-emits every time either posts or comments change.
what is the best way to do that?
the approach should be something like this:
1. split Flow<List<Post>>
into individual Flow<Post>
2. observeComments(posts.id).flatMapLatest { post.copy(comments = it) }
3. merge `Flow<Post>`s into single Flow<List<Post>>
???Mariusz Sołtysiak
06/18/2024, 9:43 PMMarko Novaković
06/18/2024, 10:08 PMList<Post>
at the end. how can I get that with merge?Mariusz Sołtysiak
06/19/2024, 5:37 AMRok Oblak
06/19/2024, 8:41 AMfold
, or specifically runningFold
which will start with an accumulator (empty mutable list) and start adding new emissions to it.Marko Novaković
06/19/2024, 2:05 PMpostsRepository
.observePosts(feedIds)
.flatMapLatest { posts ->
combine(
flows =
posts
.map { post ->
postsRepository
.observePostComments(post.id)
.mapLatest { post.copy(comments = it) }
},
transform = { it.toList() },
)
.onEmpty { emit(persistentListOf()) }
.mapLatest { it.toImmutableList() }
)
}