nwh
12/28/2019, 3:19 AMids.map { id ->
async(<http://Dispatchers.IO|Dispatchers.IO>) {
api.getMember(id) // I want to emit this
}
}
Or is there a better-suited construct for this, like a channel?octylFractal
12/28/2019, 3:21 AMflow.map { async { doSomethingToMakeList(it) } }
.buffer(8)
.map { it.await() }
.flatMap { it.asFlow() }
.flatMap { it.await().asFlow() }
is slightly smoothergetMember
returns a Flow itself, this won't work, and you should probably use something like flattenMerge
instead (read the docs on that first) + you won't need async
nwh
12/28/2019, 3:28 AMgetMember
doesn't return a Flow, it's a suspending function that returns a data object. What's your flow
object in your first response?octylFractal
12/28/2019, 3:30 AMasFlow()
on it, then you can do the map-async-buffer-map like abovenwh
12/28/2019, 3:36 AMproduce
strategy:
private suspend fun loadAll0(ids: List<String>) = coroutineScope {
produce {
ids.forEach { id ->
async(<http://Dispatchers.IO|Dispatchers.IO>) {
send(api.getMember(id))
}
}
}
}
octylFractal
12/28/2019, 3:38 AMasFlow()
just generates a "cold" stream instead, which won't start until a terminal operation like collect
is called. If you want a "hot" stream, you can use produce
+ consumeAsFlow()
iircnwh
12/28/2019, 3:39 AMoctylFractal
12/28/2019, 3:39 AMnwh
12/28/2019, 3:40 AMoctylFractal
12/28/2019, 3:41 AMbuffer
prevents that by only allowing the flow to have that many + 1 tasks runningbdawg.io
12/28/2019, 12:40 PMchannelFlow
might be what you're looking for (being able to emit from multiple jobs across different threads)