dave08
10/31/2018, 1:38 PMvar currWork: Job = null
launch { if (cancelCurrWork) currWork?.cancel() }
currWork = doWork()
?Dominaezzz
10/31/2018, 1:41 PMDominaezzz
10/31/2018, 1:42 PMdoWork
and cancel it immediately?dave08
10/31/2018, 1:43 PMdoWork()
... I need to cancel the doWork when I receive an Intent from a BroadcastReceiverDominaezzz
10/31/2018, 1:44 PMdave08
10/31/2018, 1:45 PMlaunch
the doWork
because it's running a list of these synchonously... the only reason I'm `launch`ing is to get the Job to be able to cancel it... is there a better way to do this?Dominaezzz
10/31/2018, 1:49 PMJonathan
10/31/2018, 2:18 PMsuspend fun main() {
coroutineScope {
val job = launch { ... }
job.cancel() // <-- this cancel only the child, not the parent scope
}
}
dave08
10/31/2018, 2:24 PMcoroutineScope { }
, so that I can stop that function's execution from another function in the class.
class WorkDoer(supervisorScope: CoroutineScope): CoroutineScope by supervisorScope {
var currJob: Job? = null
var currJobId: Int = 0
suspend fun doWork(id: Int) {
currJobId = id
currJob = launchInScope { .... }
}
fun cancelWork(id: Int) = if (currJobId == id) currJob?.cancel()
}
This is closer to my use case.dave08
10/31/2018, 2:34 PMcurrJob
might be a different one by the time cancel is requested on the previous one... I'm thinking of maybe using a Mutex..? 🤔Daniel Tam
10/31/2018, 3:12 PMval job = Job()
coroutineScope {
launch(job) {...}
}
dave08
10/31/2018, 3:15 PMval job = launchInScope { }
? Each piece of work needs its own Job, so that a specific piece of work with an id
can be cancelled...Daniel Tam
10/31/2018, 3:17 PMdave08
10/31/2018, 3:18 PMWorkDoer
that I posted above is very similar to my use case, thanks anyways 🙂!Daniel Tam
10/31/2018, 3:21 PMDaniel Tam
10/31/2018, 3:22 PMdave08
10/31/2018, 3:22 PMDaniel Tam
10/31/2018, 3:25 PMdave08
10/31/2018, 3:26 PMJonathan
10/31/2018, 3:26 PMI need that job reference outside theButcoroutineScope { }
coroutineScope {}
suspend until the job completes....dave08
10/31/2018, 3:27 PM