Norbi
09/09/2023, 3:20 PMrunBlocking()
in my application if the main()
function itself is suspend
?Joffrey
09/09/2023, 3:21 PMNorbi
09/09/2023, 3:25 PMCLOVIS
09/11/2023, 7:55 AMrunBlocking
from within a call stack which already has `suspend`; otherwise, you get two completely unrelated coroutine contexts.Norbi
09/11/2023, 10:05 AMnever callingThen thefrom within a call stack which already hasrunBlocking
suspend
suspend main()
solution won't be correct 😞
I have some code that creates a Koin container, and when initializing one of the singletons, it calls a suspend
operation using runBlocking()
.
What I need is to port this to multiplatform code, including Kotlin/JS - and I'm stuck at how to do it...
So basically a non-suspend library code calls my code where I need to call a suspend function. I think this is not possible to do in Kotlin/JS, so I try to find some alternative...Joffrey
09/11/2023, 10:08 AMbasically a non-suspend library code calls my code where I need to call a suspend functionThen it depends on what the library expects. Does it expect the work to be complete when the function returns? If not, then you might get away by launching an asynchronous piece of code to do the work, and return immediately. But if the caller needs a guarantee that the code has been executed before the function returns, then it's going to be more complicated.
Norbi
09/11/2023, 10:20 AMDoes it expect the work to be complete when the function returns?Unfortunatelly, yes. That's why I started to "hack" the whole bootstrap process by using a
suspend main()
, so making the coroutineScope()
function available in nested non-suspend code as well.
But probably I have to do a larger restructuring 😞
Thanks.CLOVIS
09/11/2023, 12:25 PMsuspend main
function.Norbi
09/11/2023, 3:24 PMWhat are you using Koin for, exactly?I use it extensively for (multiplatform) modularization and as a "DI framework" - so I can't drop it easily... but thanks for the idea.