petersommerhoff
04/24/2018, 7:58 AMisActive
yourself to stop execution -- all of Kotlin's suspending functions do that so they also enable cancellationDario Pellegrini
04/24/2018, 8:19 AMpetersommerhoff
04/24/2018, 8:19 AMDario Pellegrini
04/24/2018, 8:22 AMDario Pellegrini
04/24/2018, 8:22 AMDario Pellegrini
04/24/2018, 8:22 AMDario Pellegrini
04/24/2018, 8:23 AMDario Pellegrini
04/24/2018, 8:25 AMDario Pellegrini
04/24/2018, 8:26 AMpetersommerhoff
04/24/2018, 8:37 AMisActive
manually either, so that coroutine is not cooperative. At no point can it cancel. If you use delay
instead of Thread.sleep
, it becomes cooperative and can be cancelled.petersommerhoff
04/24/2018, 8:38 AMsuspend
modifier is redundant, it's just a normal function without any suspending calls. Again, this changes if you use the non-blocking delay
instead of the blocking Thread.sleep
Dario Pellegrini
04/24/2018, 10:11 AMfun getStuff(): Deferred<String> = async { ... }
I should do
deferred = async(CommonPool) {
while (true) {
Log.i("Coroutines", get().await())
}
}
Dario Pellegrini
04/24/2018, 10:11 AMDario Pellegrini
04/24/2018, 10:27 AMpetersommerhoff
04/24/2018, 11:10 AMawait()
(which I assume is suspending), it is cancellable 👍
But I'm not sure why you would call await()
on the same future again and again in a while (true)
loop.petersommerhoff
04/24/2018, 11:27 AMDario Pellegrini
04/24/2018, 11:37 AMjob = launch(UI) {
while (true) {
updateUI(get().await())
}
}
Had to update UI constantly using a service (IoT board).Dario Pellegrini
04/24/2018, 11:39 AMgildor
04/24/2018, 3:42 PMwhile(isActive)
instead, on practice there is no difference (when coroutines cancelled next call of await will stop loop), but just more explicit approachgildor
04/24/2018, 3:46 PMIf instead of that Thread.sleed I would have a network requestIt’s fine if you have suspenstion point, but don’t forget to cancel network request on coroutine cancallation, your callback adapter
await()
should handle this, otherwise network request continued after coroutines cancelation