https://kotlinlang.org logo
#coroutines
Title
# coroutines
p

Paul Woitaschek

11/16/2018, 3:37 PM
Now in rare conditions line 6 is being executed after I cancelled the job.
v

Vsevolod Tolstopyatov [JB]

11/16/2018, 3:39 PM
yes, because you can invoke
cancel
after
render
method started its execution
p

Paul Woitaschek

11/16/2018, 3:40 PM
How is that possible?
v

Vsevolod Tolstopyatov [JB]

11/16/2018, 3:40 PM
you can boil your example down to:
Copy code
// Thread 1
if (flag) {
   render()
}

// Thread 2
flag = false
do you expect
render
to be invoked when both threads are running concurrently?
p

Paul Woitaschek

11/16/2018, 3:40 PM
But what is thread 2?
v

Vsevolod Tolstopyatov [JB]

11/16/2018, 3:41 PM
oops, I’ve missed the fact that cancellation is run from the main thread
p

Paul Woitaschek

11/16/2018, 3:41 PM
consumeEach is executed on the main thread, right? So how can I cancel it on the main thread if the main thread is running?
v

Vsevolod Tolstopyatov [JB]

11/16/2018, 3:42 PM
is
render
a suspend function?
p

Paul Woitaschek

11/16/2018, 3:42 PM
No
I can catch the case and send silent crashes to my crash reporting tool.
What should I log to debug this issue?
v

Vsevolod Tolstopyatov [JB]

11/16/2018, 3:48 PM
what does
get
do?
is
viewModel
a
CoroutineScope
? Are you cancelling
job
or
viewModel
?
p

Paul Woitaschek

11/16/2018, 3:48 PM
viewModel is no CoroutineScope
fun CoroutineScope.get(billingModule: BillingModule, skuDetailSelected: ReceiveChannel<SkuDetail>): ReceiveChannel<VideoPromoState> {
t

tateisu

11/16/2018, 4:28 PM
You can check coroutineContext [Job] ?. isActive before, after, and inside render ().
p

Paul Woitaschek

11/16/2018, 4:36 PM
I can but I shouldn't have to
btw this has moved to a private conversation as I shared confidential code
3 Views