https://kotlinlang.org logo
#coroutines
Title
# coroutines
p

Pablo

01/13/2021, 8:12 AM
If I have my presenter that has a
CoroutineScope
as a parameter with dagger like
Copy code
fun provideFragmentScope(fragment: MyFragment): CoroutineScope = fragment.lifecycleScope
if on
onDestroyView
I put
lifecycleScope.coroutineContext.cancelChildren()
then is a good praxis to cancel coroutines? Note I'm not using
ViewModel
so I have to cancel it manually...
l

Lukas Sztefek

01/13/2021, 8:23 AM
There is no need to cancel coroutines manually even if you don’t use
ViewModel
. I recommend you to take a look at
fragment.viewLifecycleOwner.lifecycleScope
. This lifecycle is slightly different then default one and might suit your needs better.
p

Pablo

01/13/2021, 9:07 AM
What's the difference? @Lukas Sztefek
I mean, I was having problems because I was trying to cancel a coroutine when the view was not there so it was crashing that's why I'm trying to cancel it
p

Pablo

01/13/2021, 9:30 AM
So the thing is that on my fragment in the onDestroyView I call lifecycleScope.coroutineContext.cancelChildren() will it get the coroutines from the Presenter? I mean my presenter is the one that is doing some coroutine stuff, so from the fragment I'm cancelling them, is it correct?
11 Views