https://kotlinlang.org logo
Title
p

Pablo

10/20/2020, 10:13 AM
Hello on my presenter I'm getting a
CoroutineScope
and a
CoroutineDispatcher
is it mandatory to implement in the
onDestroy()
the cancel of them? what happens if I don't do this?
a

andylamax

10/20/2020, 10:29 AM
It is mandatory to do so so that you may not have leaking coroutines running while the scope of your presenter is destroyed
p

Pablo

10/20/2020, 10:56 AM
So, it's mandatory that on every onDestroy of an Activity/Fragment I do coroutineScope.cancel() and coroutineDispatcher.cancel()?
a

andylamax

10/20/2020, 12:23 PM
Just the scope. No need to cancel the dispather
i

Ian Lake

10/20/2020, 2:19 PM
Note that Activities and Fragments already have a scope that is cancelled for you in onDestroy(): https://developer.android.com/topic/libraries/architecture/coroutines#lifecyclescope
p

Pablo

10/20/2020, 3:19 PM
@Ian Lake  thanks for this note though, the thing is, my fragment/Activity have a @Inject lateinit var myPresenter : Presenter And this presenter already have as a parameter a 
MainScope()
 , so, even if they cancell their own scope they won't cancell the one created on my presenter, right?
i

Ian Lake

10/20/2020, 3:32 PM
Unless you hook up the
lifecycleScope
as the parent scope, it won't automatically cancel, that's correct
p

Pablo

10/21/2020, 6:46 AM
And if I use myActivity.lifecycleScope then it's not necessary to cancel in the onDestroy, right? @Ian Lake
i

Ian Lake

10/21/2020, 3:26 PM
It will do that automatically, yes
👍🏽 1