Hi everyone! I'm studying StateFlow's `.value` and...
# android
d
Hi everyone! I'm studying StateFlow's
.value
and
.update()
. Recently, it seems many places are trying to use
.update()
instead of
.value
due to concurrency issues. However, when I checked the internal code, both eventually call
updateState()
, and the
updateState()
handles synchronization through a synchronized block. Also, according to the code comments, both
.value
and
update()
perform operations atomically. If that's the case, it seems there shouldn't be any reason for race conditions due to multi-threading, so is there a specific reason why we must use
update()
? https://github.com/Kotlin/kotlinx.coroutines/blob/6c6df2b850382887462eeaf51f21f58bd982491d/kotlinx-coroutines-core/common/src/flow/StateFlow.kt#L326
f
I was under the same impression and have seen a few design pattern justified based on this assumption. Maybe this was updated in the library at some point?
d
@Fergus Hewson Yeah, In kotlin 1.5.1. there was the change.