I can launch my stateFlow collection as below
val collectingScope = CoroutineScope(Dispatchers.Default)
val stateFlow = MutableStateFlow(0)
val myJob = collectingScope.launch {
stateFlow.collect {
println("collected $it")
}
}
And I can cancel it using
myJob.cancel()
But I wonder if I can also cancel it through the stateFlow
instead?
I see there's a cancel()
function but it's deprecated
@Deprecated(
message = "cancel() is resolved into the extension of outer CoroutineScope which is likely to be an error." +
"Use currentCoroutineContext().cancel() instead or specify the receiver of cancel() explicitly",
level = DeprecationLevel.ERROR,
replaceWith = ReplaceWith("currentCoroutineContext().cancel(cause)")
)
public fun FlowCollector<*>.cancel(cause: CancellationException? = null): Unit = noImpl()
If I can do so, does the cancelation also auto-cancel myJob
?