https://kotlinlang.org logo
#coroutines
Title
# coroutines
a

Andrew

04/25/2020, 9:20 PM
I'm not sure on the proper syntax to get this to work. The error is that a suspend function can only be ran inside a coroutine scope. How do I make "runSomething" be a coroutine scope which is one the launch inside starts? runSomething { someSuspendFunction() } fun runSomething(action: () -> Unit) { viewModelScope.launch { try { busy=true action() } finally { busy=false } } }
o

octylFractal

04/25/2020, 9:21 PM
declare your
action
as
suspend () -> Unit
, i.e. a
suspend
lambda
a

Andrew

04/25/2020, 9:33 PM
Thanks, that worked
s

streetsofboston

04/26/2020, 3:59 PM
Or make your function
runSomething
be an
inline
function.
o

octylFractal

04/26/2020, 5:27 PM
that seems like the wrong thing to do here since it's using
launch
and not calling it directly.
inline
would technically work if
runSomething
was called in a
suspend
context, but if it's not it prevents legitimate calls that could use the suspend context in
launch
s

streetsofboston

04/26/2020, 9:05 PM
You're right. Looking at it again, making the function
inline
would probably need to have its
action
lamba param to be declared
noinline
, which would defeat the purpose.
2 Views