Pablo
09/27/2021, 2:02 PMlaunch{}
?
Example :
private val job: Job? = null
internal class Foo : CoroutineScope {
override val coroutineContext: CoroutineContext
get() = Dispatchers.Main + Job!!
...
private fun bar() {
job = Job()
launch { .... }
}
override onDetach() {
job.cancel()
}
}
Does it really cancel the launch?Pablo
09/27/2021, 3:11 PMOliver.O
09/27/2021, 3:30 PMjob = launch { ... }
Then job.cancel()
would cancel the launched job.myanmarking
09/27/2021, 10:52 PMget() = Dispatchers.Main + Job
. Arent you creating a new job everytime you request it ?andylamax
09/28/2021, 3:52 AMonDetach()
will cancel the whole scope, and its child coroutines, including the one launched by launch{}
Does it make sense to have a job and not assign it launch?
In some cases yes, You might need a job associated with a whole scope, not just child coroutines. But I wouldn't use your current approach to achieve that
uli
09/28/2021, 6:32 AMJob!!
was meant to be job!!
and not Job()!!
. At least if you call `bar()`only once.
Coroutine scopes organise their jobs in a hierarchical structure. So the Job created by this@CoroutineScope.launch
becomes a child job of job
. And job.cancel()
will cancel job
and all its children.
Annotations:
1. Looks confusing that job
is global. You should make it a member.
2. initialize job
centrally and once (per instance)
3. The pattern of implementing CoroutineScope
was suggested at the early times of coroutines. Nowadays it is mostly agreed to be more expressive to have a `CoroutineScpoe`member.
You probably want something like this which makes your intentions quit obvious
internal class Foo {
val fooScope = CoroutineScope(Dispatchers.Main + Job())
...
private fun bar() {
fooScope.launch { .... }
}
override onDetach() {
fooScope.cancel()
}
}
Pablo
09/28/2021, 9:19 AMPablo
09/28/2021, 9:20 AMCoroutineScope
then it's attached to the lifecycle of the class right? If the class it's a custom view in Android when it detaches from the screen it should be cancelled (in case I cancel it)myanmarking
09/28/2021, 9:22 AMPablo
09/28/2021, 9:23 AMPablo
09/28/2021, 9:24 AMuli
09/28/2021, 9:24 AMval job
Pablo
09/28/2021, 9:24 AMPablo
09/28/2021, 9:24 AMuli
09/28/2021, 9:25 AM@uli but if I implement CoroutineScope then it's attached to the lifecycle of the class right?
@Pablo take a look at the declaration of CoroutineScope. There is no lifecycle binding there. But there are androidx libraries for viewmodel scope et alPablo
09/28/2021, 9:26 AMjob = Job()
super.attachedToScreen()
And then there's a method called once when it's dettached that I want to cancel it.Pablo
09/28/2021, 9:26 AMCoroutineScope
instead of implementing it?Pablo
09/28/2021, 9:27 AMPablo
09/28/2021, 9:27 AMuli
09/28/2021, 9:28 AMuli
09/28/2021, 9:35 AMfooScope.launch { }
fooScope.cancel()
It is fully equivalent to what you tried to do. Just More explicit.Pablo
09/28/2021, 9:35 AMMainScope()
instead? What I'd like to know is, why in your approach you cancel the scope and in my approach I cancel the Job, I know if I cancel the job (from my question) I'd cancel everything is launched in the launch
, right?uli
09/28/2021, 9:35 AMCoroutineScope.cancel()
?uli
09/28/2021, 9:36 AMPablo
09/28/2021, 9:36 AMuli
09/28/2021, 9:37 AMjob
in a separate val. CoroutineScope already remembers it for youuli
09/28/2021, 9:37 AMuli
09/28/2021, 9:37 AMPablo
09/28/2021, 9:38 AMCoroutineScope
in case you want to be attached to your lifecycle of that class, right? But do I have to add it somewhere?Pablo
09/28/2021, 9:38 AMuli
09/28/2021, 9:38 AMcancel
in onDetach
uli
09/28/2021, 9:39 AMSomething like, if your class gets destroyed it autommatically cancels all the scopes on it
No. And implementing an interface also does not do thatPablo
09/28/2021, 9:40 AMuli
09/28/2021, 9:40 AMPablo
09/28/2021, 9:43 AMPablo
09/28/2021, 9:43 AMPablo
09/28/2021, 9:46 AMval fooScope = CoroutineScope(Dispatchers.Main + Job())
Than doing this
val fooScope = MainScope()
? I see that MainScope()
is creating a SupervisorJob()
instead of a Job()
. In terms of efficiently what do you think it's better?uli
09/28/2021, 9:46 AMPablo
09/28/2021, 9:47 AMuli
09/28/2021, 9:47 AMuli
09/28/2021, 9:48 AMPablo
09/28/2021, 9:49 AMlaunch{}
oncePablo
09/28/2021, 9:49 AMuli
09/28/2021, 9:50 AMJob
. If the jobs are more unrelated use a SupervisorJob
.Pablo
09/28/2021, 9:51 AMMainScope()
or just the way you typed it with Dispatchers.Main
uli
09/28/2021, 9:51 AMPablo
09/28/2021, 9:51 AMPablo
09/28/2021, 9:52 AMuli
09/28/2021, 9:52 AMuli
09/28/2021, 9:53 AMPablo
09/28/2021, 9:54 AMuli
09/28/2021, 10:04 AMAny coroutine launched in this scope is canceled when the Lifecycle is destroyed.
https://developer.android.com/topic/libraries/architecture/coroutines#lifecyclescopePablo
09/28/2021, 10:06 AMonAttachToWindows
and onDetachToWindows
I can get the lifeCycle of it (with dagger for instance and then pass it through parameter of constructor)uli
09/28/2021, 10:20 AM