Retrofit does not support Flow. So if I need to me...
# coroutines
a
Retrofit does not support Flow. So if I need to merge concurrent requests using 'flatMapMerge' what is the right way to do this?
d
You don't need
Flow
for this.
You just need
List<Deferred<T>>.awaitAll()
.
You can do something like,
Copy code
val requests = listOf(
    async { api.getUser() },
    async { api.getComments() }
)
val responses = requests.awaitAll()
l
Also, make sure to wrap these in a local
coroutineScope { … }
and handle errors around it.
a
Flow
has great API and I need
ConflatedBroadcastChannel
for some reasons. What i did:
Copy code
flowOf(StreamType.values().asFlow()
                        .flatMapMerge(10) { stream ->
                            flowOf(stream to async { apiService.getCurrentInfo(stream.path) })
                        }
                        .map { Pair(it.first, it.second.await()) }
                        .fold(mutableMapOf(), { map: MutableMap<StreamType, ContentMetadataDto>, pair: Pair<StreamType, ContentMetadataDto> ->
                            map.apply { put(pair.first, pair.second) }
                        }))
                        .onEach { map ->
                            contentMetadata.send(map)
                        }
                        .catch {
                            emit(StreamType.values().map { it to ContentMetadataDto.NO_DATA }.toMap(mutableMapOf()))
                        }
                        .collect {
                            contentMetadata.send(it)
                        }
d
I think you can replace the first
flowOf
with
Copy code
StreamType.values().associateWith { stream -> async { apiService.getCurrentInfo(stream.path) } }.mapValues { it.second.await() }
(I'm not sure, it's hard to read).
Copy code
flowOf(StreamType.values().asFlow()
    .flatMapMerge(10) { stream ->
        flowOf(stream to async { apiService.getCurrentInfo(stream.path) })
    }
    .associate { Pair(it.first, it.second.await()) }))
    .onEach { map ->
         contentMetadata.send(map)
    }
    .catch {
         emit(StreamType.values().associateWith { ContentMetadataDto.NO_DATA }
    }
    .collect {
        contentMetadata.send(it)
    }
a
yeap, but this works fine - thank you)
d
Also you can use
associateWith