YYY
YYY

Reputation: 1

How to fetch User object for each id in Flowable<List<String>>

I have a Flowable of a List of ids that represent user ids in a group.

private val memberIdsFlowable: Flowable<List<String>> = groupFlowable
        .map { it.members }

I need to call

fun fetchUserFlowable(id: String): Flowable<User>

For each id in the list and combine them to a list of users Flowable<List<User>>

What I've tried is this:

private val membersFlowable = memberIdsFlowable
    .concatMap { list ->
        Flowable.fromIterable(list)
            .map { userRepository.fetchUserFlowable(it) }
    }

But it results in Flowable<Flowable<User>> rather than Flowable<List<User>> and if I add toList() operator then it's a type mismatch :

   private val membersFlowable = memberIdsFlowable
    .concatMap { list ->
        Flowable.fromIterable(list)
            .map { userRepository.fetchUserFlowable(it) }
            .toList()
    }

Upvotes: 0

Views: 207

Answers (1)

Alexey Romanov
Alexey Romanov

Reputation: 170805

You simply need concatMap inside as well:

private val membersFlowable = memberIdsFlowable
    .concatMap { list ->
        Flowable.fromIterable(list)
            .concatMap { userRepository.fetchUserFlowable(it) }
    }

So that the lambda returns Flowable<User> for each list.

EDIT: if you want one List<User> for each List<String> in memberIdsFlowable, the outer operation should be map. I am less sure about its argument, but I think something like this should work:

private val membersFlowable = memberIdsFlowable
    .map { list ->
        Flowable.fromIterable(list)
            .concatMap { userRepository.fetchUserFlowable(it) }
            .toList()
    }

Upvotes: 1

Related Questions