Don Mitchell
04/23/2025, 9:33 PMmain
? e.g., when implementing com.github.ajalt.clikt.core.CoreCliktCommand#run
?
You can't
CoroutineScope().launch {
...
}
as there's nothing to stop the process from exiting and the container from shutting down before it does anything (there's no containing scope). You can't call join
on that bc there's no suspend context.
I've been wantonly using runBlocking
but I know that's not the preferred pattern. What I don't grok is what is the preferred pattern? (NOTE, this is a long-running headless batch service; so, there's no UI so MainScope
makes no sense, coroutineScope
is not allowed bc it's not a suspend context, GlobalScope
has the same problem as CoroutineScope()
, neither of these exist: lifecycleScope, viewModelScope
)kevin.cianfarini
04/23/2025, 11:08 PMSam
04/24/2025, 7:01 AMSam
04/24/2025, 7:03 AMCoreCliktCommand
for a SuspendingCliktCommand. It has a suspending run()
function, inside which you'd be able to use the coroutineScope()
function.Sam
04/24/2025, 7:06 AMrunBlocking
would also likely be okay in that specific scenario too, since this is essentially an entrypointJoffrey
04/24/2025, 8:08 AMSuspendingCliktCommand
in Amper so we have suspending entrypoints. You just have to make sure all of them are suspending, it's not possible to mix.Joffrey
04/24/2025, 8:14 AMrunBlocking
when Clikt didn't have that, and I agree with Sam and Kevin that this is one of the rare cases where runBlocking
is fine