Can I do the follwing, when using `flow` and `stat...
# coroutines
b
Can I do the follwing, when using
flow
and
stateIn
or does it has unpredictable side effects. It does not feel right to pass
this
into
stateIn
. Does
doSomething()
ever return or will it suspend infinite, because the scope is used by
stateIn
?
Copy code
suspend fun doSomeThing() = coroutineScope {
    val someState = someFlow.stateIn(this)
    ...
}
l
Suspend infinite
m
i don’t think it will suspend infinite, but i’m not sure. When caller scope gets cancelled, this will get cancelled to no ?? If not, please explain why - i would like to understand this as well
l
Suspend infinite that can be cancelled, but reaching the end of the
coroutineScope
lambda, doesn't cancel the scope. What happens instead is it waits for all children to complete, which will never happen.
The only thing that can happen besides a crash is external cancellation.
b
Is there another way to use scope, that depends on the outer scope, where
doSomething()
is called?
l
You need to pass that scope as a parameter, or get a reference to the one you want in another way
b
Shouldn't it be possible to open a new scope using
currentCoroutineContext()
, so it will be cancelled when the parent from
currentCoroutineContext()
is cancelled? Or is this all irrelevant, because the garbage collector cancels the
StateFlow
when there is no reference to it anymore?
l
The GC doesn't cancel the StateFlow
Using currentCoroutineContext() for that is quite unsafe, and lacks explicitness