dimsuz
12/08/2021, 12:57 PMinterface Service {
fun start(): Job
}
val job = myServiceInstance.start()
job.cancel()
is it ok to implement start like
fun start() { val scope = createScope(); return scope.coroutineContext[Job] }
Or is there some better way?streetsofboston
12/08/2021, 1:10 PMinterface Service {
fun start()
fun cancel()
}
myServiceInstance.start()
myServiceInstance.cancel()
Where myServiceInstance contains a (private) CoroutineScope that is used by start and cancelled by cancel.dimsuz
12/08/2021, 1:15 PMelizarov
12/08/2021, 2:21 PMJob as a thing you can use to cancel the service. It is explicitly designed for such use. (Btw, in the prototype design it was called a Lifetime)elizarov
12/08/2021, 2:22 PMJob (and a scope) even before that start is called to keep them in private `val`s inside your class.elizarov
12/08/2021, 2:23 PMstart will then launch some coroutines in your scope and will simply return the job of the scope.elizarov
12/08/2021, 2:23 PMJob that launch returns.dimsuz
12/08/2021, 2:43 PMThe great thing is that you can create thisOh, nice, this is something I should try! Currently I mess with nullable(and a scope) even before thatJobis called to keep them in private `val`s inside your class.start
_scope etc which is not as great as not doing this dance 🙂 I do launch several coroutines, so it's the first option for me.streetsofboston
12/08/2021, 4:01 PM