I have seen a lot of examples that use a job as a way to cancel coroutines when viewmodel is destroyed.
class SetupViewModel : ViewModel() {
private val completableJob = Job()
private val coroutineScope = CoroutineScope(Dispatchers.IO + completableJob)
override fun onCleared() {
super.onCleared()
completableJob.cancel()
}
}
The strange thing for me is no one seems to be using coroutineScope to do that. I think that should be easier and has less code. Any idea?
class SetupViewModel : ViewModel() {
private val coroutineScope = CoroutineScope(Dispatchers.IO)
override fun onCleared() {
super.onCleared()
coroutineScope.cancel()
}
}