are inline suspend parameters planned? e.g. when I...
# coroutines
k
are inline suspend parameters planned? e.g. when I want to execute some suspend lambda but want to wrap it in some
use
style code but don't want additional allocations.
e
k
thx
e
k
@elizarov actually, I was thinking of something slightly different:
Copy code
inline fun launchWithDismiss(block: suspend () -> Unit) = launch(handleExceptions = true) {
    try {
        block()
    } finally {
        dismissAllowingStateLoss()
    }
}
is this also covered by the issue?
e
That works now if you add “noinline” modifier before the
block
param. Should also work without “noinline” modifier if the issue I’ve quoted is fixed.
I’m add to the comments
k
There's no difference in allocations between
inline
with
noinline
and leaving out
inline
altogether, right?
e
Nope.
Btw, if all you want to do is to handle exception inside
launch
, then you don’t need it.
You need to define your own
CoroutineExceptionHandler
k
I know, that's not what I want to do. What I want to do is dismiss a dialog, no matter how the coroutine terminates
ah I could do this with a context too, right?
e
You cannot do this with a context, but I think there is a better way to do it with coroutines:
Copy code
val job = launch(context) { … doSomething ... }
launch(context + NonCancellable) { // !!! 
    job.await()
    dismissAllowingStateLoss()
}
k
In which way is this better? allocation-wise? Or elegance-wise?
e
Elegance-wise, of course
k
but isn't my code pretty readable itself?
e
Actually, my version does slightly different thing, which may make it nonappropriate for your needs
Your code invokes
dismiss
when the original code completes (including whatever other cleanup there was), while my code invokes
dismiss
as soon as the job is cancelled (it may invoke
dismiss
before
doSomething
code had terminated)
k
yeah, I'll go with my version for now
d
pretty dangerous. what if activity/fragment has been destroyed before job is done?
k
@deviant this is a
DialogFragment
that has
retainInstance = true
the whole job of the class is to run some code no matter what the activity lifecycle
d
ok then 🙂
k
it's a pretty neat idiom, if I dare say so. the user has some visual feedback that some work is being done and I don't have to take care of lifecycle issues