https://kotlinlang.org logo
#compose
Title
# compose
c

Colton Idle

01/31/2023, 10:44 PM
I'm reading https://medium.com/androiddevelopers/effective-state-management-for-textfield-in-compose-d6e5b070fbe5 which says
Even just using reactive streams to represent your state (e.g.
StateFlow
) without delays might cause issues as the update event dispatch is not immediate if you’re using the default dispatcher.
Maybe this is more of a corotuines question, but if you're using the default dispatcher (which is main on android?) wouldn't that be immediate?
👀 1
j

jw

01/31/2023, 11:19 PM
Main always still posts. Main.immediate is the one which does not post if you already are on the main thread
c

Colton Idle

02/01/2023, 12:19 AM
TIL theres Main and Main.immediate 🙈
Also, maybe dumb question but what do you mean by "still posts"? Like goes into a queue?
f

Francesc

02/01/2023, 12:43 AM
the main point behind that post is that you should only update the state for a text field synchronously, you should not update by passing a message to the viewmodel indicating "input updated", which goes to a queue and then is processed, creates a new state object which gets published to the UI, which ends up updating the actual display of the text field. You can still post stuff to the viewmodel, but you should update the field state synchronously to avoid issues
the way I've solved this is by creating a StateHolder to host the input field state
e

efemoney

02/01/2023, 12:46 AM
Or use mutable state in your view model
f

Francesc

02/01/2023, 1:09 AM
that works if you have a MVVM approach, but if you opt for MVI, where you are posting messages that end up in your reducer, that won't work here
l

Landry Norris

02/01/2023, 2:57 PM
It’s also best to avoid using compose state in your view model if using multiplatform. StateFlow is much easier to use with non-compose UI libraries.
c

Colton Idle

02/01/2023, 3:02 PM
i like compose state because its so much easier to work with than stateFlow
¯\_(ツ)_/¯
l

Landry Norris

02/01/2023, 3:04 PM
Not quite sure why they say to avoid flows, though. I’ve always used a StateFlow with TextFields and haven’t seen any issues. I always make sure that methods immediately update the flow, without any intermediate steps like in their first example.
It seems like the main issue was the async operation between. It seems like it would be easier when working with flows to have your validation step map from the text state flow as an easy way to make sure it doesn’t slow down the text state.
c

Colton Idle

02/01/2023, 3:50 PM
Yeah. I've used stateFlow with textField once and didn't see any issues.
but the async stuff makes complete sense. you want that state to be updated ASAP. It was a great article though. TIL i learned that with text field you end up having 3 pieces of state 🤯
f

Francesc

02/01/2023, 5:51 PM
if you don't update synchronously you may be missing characters and the cursor may jump out of place
e

efemoney

02/01/2023, 5:57 PM
that works if you have a MVVM approach
It works anywhre ie not limited to architecture 🤔. We use an mvi variant / udf …
f

Francesc

02/01/2023, 6:48 PM
we are getting into the weeds here which is not what the discussion is about, but if you are mutating the state, that's not how I understand and use MVI - in MVI you create a new state based on existing state, and there is a pipeline for the intents to travel to before they are reduced, which is not synchronous.
95 Views