Christophe Smet
01/21/2020, 6:34 PMZach Klippenstein (he/him) [MOD]
01/21/2020, 6:48 PMflatMapMerge
takes a concurrency
parameter that defines how many upstream Flows may be collected simultaneously: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/flat-map-merge.htmlDEFAULT_CONCURRENCY
is 16 by default, but may be overridden by a system property: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-d-e-f-a-u-l-t_-c-o-n-c-u-r-r-e-n-c-y.htmlChristophe Smet
01/21/2020, 6:54 PMThis operator calls transform sequentially
My code maps a list of ip address to a rest result:
return generateAddressesInSubnet(preferredInterfaceAddress) //returns a list of inetAddresses
.asFlow()
.flatMapMerge() {
val url = "http://${it.hostAddress}/status"
try {
logger.debug { "Connecting to $url" }
val apiResponse = discoveryApiService.fetchStatus(url)
flowOf(LanDiscoveryResult(it, apiResponse))
} catch (ex: Exception) {
emptyFlow<LanDiscoveryResult>()
}
}
These rest calls should run in parallel with that code right ?octylFractal
01/21/2020, 7:04 PMChristophe Smet
01/22/2020, 9:49 AMZach Klippenstein (he/him) [MOD]
01/22/2020, 7:44 PMObservable.just(expensiveFunction())
When the author really wanted this:
Observable.fromCallable { expensiveFunction() }