```data class Comment(...) data class Post(val com...
# getting-started
m
Copy code
data 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>>
???
m
m
I need
List<Post>
at the end. how can I get that with merge?
m
Sorry, I misunderstood what you try to achieve
r
for combining individual emissions into a collection, you can use
fold
, or specifically
runningFold
which will start with an accumulator (empty mutable list) and start adding new emissions to it.
m
this worked and it’s passing all the tests:
Copy code
postsRepository
      .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() }
        )
      }