Hi! I’m practicing Compose for desktop and I’m stu...
# compose-desktop
s
Hi! I’m practicing Compose for desktop and I’m stuck with the problem: I have “screenState: StateFlow” value with state for my UI (sealed class) From UI I use default approach “screenState.collectAsFlow()” to trigger recomposition if state changes and I have method “subscribe(chatRoom: ChatRoom)” in my ApplicationController (aka view model for desktop) which calls to a useCase and returns Flow<Message>. Emitter takes my state and makes a copy with a new message but from time to time Compose ignores (or misses) new state value. Screenshots in thread.
Compose state
State flow in app controller
chat message emitter
z
You need to copy the list as well, or use
SnapshotStateList
1
Some other notes: 1. It’s considered a bad practice to implement
CoroutineScope
directly from classes that do other things. Store the scope in a property instead (elizarov has written about this). 2. No need to pass
Dispatchers.Main
to
collectAsState
, it will use the compose dispatcher by default (which is basically
Dispatchers.Main
with extra features). 3. There’s a potential data race in your
subscribe
coroutine, if there are multiple instances of that coroutine running concurrently. One way to fix would be wrap the body of the
collect
with a
Mutex
.
s
Noted) Problem solved, thanks a lot!
No need to pass 
Dispatchers.Main
 to 
collectAsState
That’s just attempt to fix the problem by changing something random 😄