https://kotlinlang.org logo
Title
v

Vincent Williams

07/29/2020, 10:07 PM
Are these two code snippets equivalent?
val job = SupervisorJob()
val lifecycleScope = CoroutineScope(Dispatchers.Main + job)

lifecycleScope.launch { }
val job = SupervisorJob()
val lifecycleScope = CoroutineScope(job)

lifecycleScope.launch(Dispatchers.Main) { }
o

octylFractal

07/29/2020, 10:10 PM
I would say not entirely equivalent -- the two `lifecycleScope`s obviously have different final contexts, but the code in
launch
will be run in the same way, if that was the question
v

Vincent Williams

07/29/2020, 10:12 PM
ok that was my main question. Why would the context be different? would it need to be
lifecycleScope.launch(Dispatchers.Main + job) { }
ie: if I cancel job in the second snippet, it still cancels that
launch
correct?
o

octylFractal

07/29/2020, 10:13 PM
the context is different in that any
launch
(or async, etc.) would use the Main dispatcher by default in the first one, but only the single launch in the second one uses Main by default
👍 3
the job cancellation would still work the same
only the
coroutineContext[CoroutineInterceptor]
would differ -- no other element
1
v

Vincent Williams

07/29/2020, 10:14 PM
ok perfect! thank you!