bsimmons
10/28/2020, 6:35 PMwhile
seems like an antipattern to me.) Any ideas?louiscad
10/28/2020, 6:40 PMraceOf
function exactly for that in #splitties, the documentation is here: https://github.com/LouisCAD/Splitties/tree/main/modules/coroutines#racing-coroutinesbsimmons
10/28/2020, 6:46 PMntherning
10/29/2020, 7:21 AMselect
would work?
Select expression makes it possible to await multiple suspending functions simultaneously and select the first one that becomes available.
https://kotlinlang.org/docs/reference/coroutines/select-expression.html#selecting-deferred-valueslouiscad
10/29/2020, 8:31 AMraceOf
Nick
10/29/2020, 1:36 PMflowOf(
callbackFlow<String> {
Timber.e("starting request 1")
delay(1000)
offer("Request 1 completed")
invokeOnClose {
Timber.e("Request 1 is now closed")
}
},
callbackFlow<String> {
Timber.e("starting request 2")
delay(500)
offer("Request 2 completed")
invokeOnClose {
Timber.e("Request 2 is now closed")
}
}
)
.flattenMerge()
.first()
.also {
Timber.e(it)
}
Results are:
starting request 1
starting request 2
Request 2 is now closed
Request 2 completed
But I expected Request 1 to get closed at the end too. Do you guys think this is a bug?bsimmons
10/29/2020, 2:06 PMselect
was the way to go to clean things up.
@Nick Not sure what the problem you're having is, but here's the final solution I came up with if that helps. (It's a bit of a simplification of Louis' code.)Nick
10/29/2020, 2:09 PMbsimmons
10/29/2020, 2:13 PMNick
10/29/2020, 2:16 PMlouiscad
10/29/2020, 2:33 PMbsimmons
10/29/2020, 2:50 PMCancellationException
in onAwait
?louiscad
10/29/2020, 2:55 PMcoroutineScope { }
so async
calls use that scope.bsimmons
10/29/2020, 3:09 PMasync
by default uses a default/global scope? I never though about that. I guess in the general case, most suspend functions should be like this? (If they need to respect the current scope?)
suspend fun foo() = coroutineScope { ... }
louiscad
10/29/2020, 3:09 PMlaunch
, async
, or other means, yes.bsimmons
10/29/2020, 3:10 PM