Reputation: 1491
Is coroutineContext[Job]!!
safe to use inside a suspend
function? Do all suspend functions have a Job?
Upvotes: 3
Views: 1030
Reputation: 3207
No, not all suspend functions have a job.
coroutineContext
is general Kotlin mechanism to pass coroutines-related objects through suspend
call-chain transparently and Job
is kotlinx.coroutines
specific abstraction.
If you've launched a coroutine via any kotlinx.coroutines
builder which supports Job
(e.g. using async
, launch
or produce
. You can check specific builder documentation to check whether it supports Job
), then you have Job
in your coroutineContext
.
But if you launched coroutine in another way, e.g. using stdlib buildSequence
, then coroutine context will not contain job.
Upvotes: 1