Robert Jaros
12/14/2022, 3:55 PMsend()
or trySend()
to dispatch inputs?Casey Brooks
12/14/2022, 4:04 PMsend()
so it suspends if the buffer is full to give your app backpressure, but trySend()
is there for cases where you’re sending an Input to the ViewModel from a non-suspending context (like a Compose callback or Android OnClickListener)Robert Jaros
12/14/2022, 4:07 PMtrySend()
from the button click listener and it doesn't work, the user will have the feeling that the button does not work?Casey Brooks
12/14/2022, 4:13 PMsend()
. trySend()
returns a ChannelResult
that can give you immediate feedback on whether the Input will be processed or was dropped, so you can respond in the UI, if needed.
However, the best situation is obviously that Inputs are not silently dropped, and that they’re handled immediately so there’s no lag between the click and the processing of that Input. There are several different Input Strategies you can choose from to help tailor exactly what happens when a new Input is sent to the ViewModel.
The default is LifoInputStrategy
, where any currently-running work is cancelled so new Inputs are processed immediately, and in this strategy trySend()
will never fail and send()
will never suspendCasey Brooks
12/14/2022, 4:20 PMBallastViewModelConfiguration.Builder()
.apply {
inputStrategy = FifoInputStrategy()
}
.withViewModel(...)
.build()