If I make modifications (`add`, `remove`) on `Muta...
# coroutines
z
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
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
Oh, ok... so even iteration over list is problematic. Thank you.
u
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
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.