Does anyone knows if I have a custom view that ext...
# coroutines
p
Does anyone knows if I have a custom view that extends
ConstraintLayout
is it good to implement the
CoroutineScope
? Or it's better to create a scope
Copy code
private val scope = MainScope()
And use it and cancel it depending on if the view is visible or not (attached/detached). The thing is that I'm doing some things and painting the custom view so I'm using a CoroutineScope for this but I'm afraid that if the CustomView is gone the coroutine is not and then try to do something and crash the app. Have anyone of you faced something similar? I used to inject the lifecycle of the "parent" let's say an
Activity
or a
Fragment
and once this is gone the coroutine is cancelled automatically.
Implementing the
CoroutineScope
interface is like creating my own lifecycle? I've read long time ago that if a view has its own lifecycle I should implement
CoroutineScope
otherwise there are other ways...
s
r
But why do you need a coroutineScope in a constraint Layout?
n
In general, I've noticed recommendations shift to using a property instead of implementing
CoroutineScope
. https://proandroiddev.com/why-your-class-probably-shouldnt-implement-coroutinescope-eb34f722e510 https://kt.academy/article/cc-constructing-scope