I'm not sure on the proper syntax to get this to w...
# coroutines
a
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
declare your
action
as
suspend () -> Unit
, i.e. a
suspend
lambda
a
Thanks, that worked
s
Or make your function
runSomething
be an
inline
function.
o
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
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.