Hi All, it might seem trivial, but I wanted to ask...
# coroutines
s
Hi All, it might seem trivial, but I wanted to ask, shall cancel CoroutineScope in ViewModel$onCleared or shall I cancel in Activity$onDestroy. What is recommended???
d
ViewModel scope must be cancelled in
onCleared()
Activity/Fragment scope must be in
onDestroyed()
But with AndroidX, ktx extensions you have
viewmodelScope
and
lifecycleScope
which are automatically instanciated and cancelled
s
But if activity is tied to viewmodel then whats the issue in cancelling scope in viewmodel oncleared. Considering the case where screen orientation changes, coroutines called in CoroutineScope will be cancelled if I call in Activity$onDestroy
d
Coroutines launched from ViewModel won't care about Activity recreation
s
Thanks, I got the picture why corotuineScope launched from viewmodel should be cancelled on onCleared.