Erik
01/23/2020, 10:35 AMsend
to it.Erik
01/23/2020, 10:38 AMoffer
is synchronous, non suspending
- offer
returns a boolean, i.e. if the channel accepted the offered item or not (buffer full)
- send
is a suspend fun
- send
either returns quickly if item accepted by channel (buffer not full), or suspends the caller if not accepted, until the item can be acceptedErik
01/23/2020, 10:38 AMErik
01/23/2020, 10:38 AMErik
01/23/2020, 10:40 AMErik
01/23/2020, 10:42 AMsend
. Then up to the buffer capacity of state actions can be buffered, any additional send
events will suspendErik
01/23/2020, 10:42 AMErik
01/23/2020, 10:44 AMsetState { LoadingState }
, that would work just fine.
Then I load my data and after that succeeds, then I'd setState { DataState }
, but the buffer now is full! So that state would never be observed by my UI if offer
is used. Eventually, my UI would stay in the Loading
state, possibly forever.Erik
01/23/2020, 10:44 AMErik
01/23/2020, 10:45 AMsend
instead of offer
Erik
01/23/2020, 10:45 AMErik
01/23/2020, 10:46 AMErik
01/23/2020, 10:47 AMMarcin Chrapowicz
01/23/2020, 10:48 AMErik
01/23/2020, 10:49 AMMarcin Chrapowicz
01/23/2020, 10:56 AMarnaud.giuliani
01/23/2020, 11:03 AMarnaud.giuliani
01/23/2020, 11:04 AMErik
01/23/2020, 11:24 AMactor
is created on AndroidDataFlow.coroutineScope
, so there is one actor only, operating in the same scope.
From SendChannel.send
KDoc:
All elements that are sent over the channel are delivered in first-in first-out order.That does mean parallel user flows might happen somewhat simultaneously, but the individual flows themselves happen in order. Can you come up with an example where that would be problematic? E.g. the user presses a button once, but before all subsequent state actions are handled, they press it again a number of times? All subsequent
actor.send(action)
calls would suspend (if buffer full), but they'd suspend in order.
So how could suspended send(action)
calls become out of order? I think using offer(action)
achieves exactly the same concurrent and in-order behaviour, with the difference that if the buffer is full then nothing is offered at all.arnaud.giuliani
01/24/2020, 1:26 PMhe difference that if the buffer is full then nothing is offered at all.insteresting 👍
Erik
01/24/2020, 1:51 PMoffer
for good reasons, then at least let the user know that something went wrong (other than just logging an error), e.g. throw an exception, return null
or false
, or a callbackarnaud.giuliani
01/24/2020, 3:50 PM