ar-g
04/20/2023, 8:53 AMasync
branch of the following code doesn't get executed:
override suspend fun loadBalances(assets: List<...>): List<...> = coroutineScope {
awaitAll(
async { loadA(assets) },
async { loadB(assets) },
).flatten()
}
However, when I use the following code with delay()
functions, it works as expected, which indicates that the issue might not be with the coroutines setup:
override suspend fun load(assets: List<...>): List<...> = coroutineScope {
awaitAll(
async {
delay(200)
listOf(...)
},
async {
delay(500)
listOf(...)
},
).flatten()
}
Additionally, the problematic code works fine in an Android project with okHttp. I suspect the issue might be related to dispatchers, but I'm not sure where to make adjustments. Any advice or guidance on this issue would be greatly appreciated!Aleksei Tirman [JB]
04/20/2023, 9:50 AMar-g
04/20/2023, 10:09 AMlifecycleScope.launch {
val balances = loadBalances()
val tvLog = findViewById<TextView>(R.id.tv_log)
tvLog.text = balances
}
ar-g
04/20/2023, 10:11 AMprivate fun <T> exportPromise(
context: CoroutineContext = EmptyCoroutineContext,
start: CoroutineStart = CoroutineStart.DEFAULT,
block: suspend CoroutineScope.() -> T,
): Promise<T> =
GlobalScope.promise(context, start, block)
.catch { throw it.toJsRpcError() }
Joffrey
04/20/2023, 1:22 PMloadA
and loadB
? Are those suspend functions?Joffrey
04/20/2023, 1:23 PMThe problem is that the secondDoes the first one complete? Does it succeed? Fail?branch of the following code doesn't get executedasync
Joffrey
04/20/2023, 1:25 PMloadBalances()
? You said it works on Android, what is the caller in the case where it doesn't work?ar-g
04/20/2023, 4:39 PMloadA
and loadB
are both POST calls.
2. The first function loadA
is fully complete, loadB
runs and stops once it reaches the Ktor POST call
3. They don't use any custom Dispatchers.
4. This code doesn't work on Android and JS
5. This code is working on a different Android project with okHttp, TBH it's unrelated to this.ar-g
04/20/2023, 4:41 PMHttpClientEngineFactory<OkHttpConfig> {
private const val MAX_IDLE_CONNECTIONS = 20
private const val IDLE_TIMEOUT = 5L
private val connectionPool = ConnectionPool(MAX_IDLE_CONNECTIONS, IDLE_TIMEOUT, TimeUnit.SECONDS)
private val dispatcher = Dispatcher()
override fun create(block: OkHttpConfig.() -> Unit): HttpClientEngine {
val defaultHttpConfig = OkHttpConfig().apply {
config {
connectionPool(connectionPool)
dispatcher(dispatcher)
}
}
return OkHttpEngine(defaultHttpConfig.apply(block))
}
}
ar-g
04/21/2023, 12:08 PM