How would you handle a coroutine which has two pos...
# coroutines
r
How would you handle a coroutine which has two possible cancel paths? I've got some callback code which can either be cancelled by the viewmodel going out of scope, or the remote device disconnecting. Have it inherit from one, and manually cancel it in the other case?
v
I inherit and manually fail the connection job (see snippet below)
I get various warnings about
coroutineContext
being ambiguous and
withContext
w/ a job may cause structured concurrency violations, however (the code is valid). I'm a bit frustrated about inner + outer cancellation being poorly so supported
Untitled.kt
r
Aye, makes sense.
o
Usage of 'kotlin.coroutine.coroutineContext' can be ambiguous
This is easily solved by applying the quick fix to use
currentCoroutineContext()
instead.
☝️ 1