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

zokipirlo

12/06/2018, 3:36 PM
If I make modifications (
add
,
remove
) on
MutableList
only in
actor
which runs in
Dispatcher.MAIN
, is it safe to call
contains
in other context (
<http://Dispatcher.IO|Dispatcher.IO>
) or should rather check that in
withContext(Main)
before sending to
actor
?
b

bdawg.io

12/06/2018, 4:03 PM
It's shared mutable state any time you access or modify a mutable structure from multiple locations. In your case, you should have a message sent to your actor that requests if that structure
contains
an element
z

zokipirlo

12/06/2018, 4:31 PM
Oh, ok... so even iteration over list is problematic. Thank you.
u

uli

12/06/2018, 4:55 PM
Even if it was safe to call.... what do you do with the result fo
contains
? On your next line of code the result might not be valid any more.
z

zokipirlo

12/07/2018, 7:14 AM
Yes, but only one thread (actor) can modify it, so it won't be a big problem I guess with list consistency. I'm just checking for contains in background thread because it's processing a lot of messages and it need to handle it only once normally and don't want to "wake" actor in 99% of cases when not necessary.
3 Views