Ravin Jain
10/29/2021, 1:00 AMcoroutineScope gets cancelled when the Job created from viewmodelscope.launch is cancelled ?Ravin Jain
10/29/2021, 1:05 AMinit {
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() insteadephemient
10/29/2021, 1:11 AMRavin Jain
10/29/2021, 1:12 AMephemient
10/29/2021, 1:12 AMephemient
10/29/2021, 1:17 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 AMproducerScopegildor
10/29/2021, 4:56 AMK Merle
10/29/2021, 5:56 AMabc().last() and make flow run only once as a suspend function.K Merle
10/29/2021, 6:00 AM.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.