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

robin

03/15/2017, 2:33 PM
@elizarov Good catch, thanks! Right now I'm only calling it from UI thread, but who knows what else I'll do with it once I have forgotten about that. I've updated the snippet, is it sufficient like that or does the standard synchronization not play nice with coroutines in some way?
e

elizarov

03/15/2017, 2:40 PM
It is Ok to use synchronization for the cases like that. You are doing it only from the outside of coroutines. There are no locks in the coroutines themselves, so you cannot accidentally run into deadlock with coroutines machinery. Don't use this approach with
Unconfined
dispatcher, though, because with unconfined dispatcher a
cancel
invocation can start executing the finally sections of the block and doing this under your
synchronized
section might cause you problems down the road with the order between other locks you might be using in your app. If you are into the defensive-programming mindset, I'd recommend moving
cancel
invocation outside of
synchronized
block.
r

robin

03/15/2017, 2:59 PM
Great explanation, thank you! I will keep it in mind.
3 Views