Ravin Jain
10/29/2021, 1:00 AMcoroutineScope
gets cancelled when the Job created from viewmodelscope.launch
is cancelled ?init {
a = viewModelScope.launch {
abc()
}
}
fun abc() = flow<Boolean> {
val b = coroutineScope {
// some operations
emit(true)
}
}
override fun onCleared() {
super.onCleared()
a.cancel()
}
When I do a.cancel
will b be cancelled automatically ?ephemient
10/29/2021, 1:08 AMflow()
is not designed for that, use channelFlow()
insteadRavin Jain
10/29/2021, 1:12 AMephemient
10/29/2021, 1:12 AMRavin Jain
10/29/2021, 1:18 AMflow {}
ephemient
10/29/2021, 1:18 AMRavin Jain
10/29/2021, 1:25 AMlaunch
inside channelFlow and then use the coroutineScope
Will the coroutine scope inside launch will be same as the flow's coroutineScope (viewmodelScope)ephemient
10/29/2021, 1:29 AMRavin Jain
10/29/2021, 1:40 AMproducerScope
gildor
10/29/2021, 4:56 AMK Merle
10/29/2021, 5:56 AMabc().last()
and make flow run only once as a suspend function..last()
will cancel if viewModelScope gets canceled. You could create custom CoroutineScope
and inject it into your ViewModel and use it. This way you'll be able to send that event when your ViewModel onCleared
gets called.