spierce7
02/05/2019, 6:14 PMSubject
with kotlinx.corotuines? There are too many edge cases with Channel
where I risk losing events, and having to deal with them all the time is exhausting.Allan Wang
02/05/2019, 6:14 PMAllan Wang
02/05/2019, 6:14 PMspierce7
02/05/2019, 6:15 PMChannel
, I mean BroadcastChannel
.spierce7
02/05/2019, 6:16 PMstreetsofboston
02/05/2019, 6:22 PMlaunch { channel.send(item) }
instead of channel.offer(item)
? Would that remember the last value sent?spierce7
02/05/2019, 6:23 PMstreetsofboston
02/05/2019, 6:25 PMoffer
is not guaranteed to deliver the message (it will return false
if the message wasn’t delivered)spierce7
02/05/2019, 6:25 PMsend
guaranteed to deliver the message on all forms of BroadcastChannel
?streetsofboston
02/05/2019, 6:25 PMstreetsofboston
02/05/2019, 6:26 PMspierce7
02/05/2019, 6:26 PMstreetsofboston
02/05/2019, 6:27 PMlaunch
will guarantee that the send
call happens in the Dispatcher associated with the CoroutineContext of the launch’s scopespierce7
02/05/2019, 6:27 PMsend
, we would only receive the most recent event, and would still lose eventsJoris PZ
02/05/2019, 6:27 PMJoris PZ
02/05/2019, 6:27 PMspierce7
02/05/2019, 6:29 PMstreetsofboston
02/05/2019, 6:29 PMstreetsofboston
02/05/2019, 6:30 PMsend
without wrapping it in a launch
.spierce7
02/05/2019, 6:31 PMstreetsofboston
02/05/2019, 6:32 PMAllan Wang
02/05/2019, 6:32 PMspierce7
02/05/2019, 6:32 PMAllan Wang
02/05/2019, 6:33 PMstreetsofboston
02/05/2019, 6:34 PMChannel
subclass, to which other (regular) channels can subscribe. Using select
on that channel, you could receive incoming events and call send
on all subscribed channels.streetsofboston
02/05/2019, 6:35 PMAllan Wang
02/05/2019, 6:35 PMproduce
, the api is going to be obsolete with changesspierce7
02/05/2019, 6:36 PMspierce7
02/05/2019, 6:37 PMoffer
was renamed to something with unsafe
in the name though.streetsofboston
02/05/2019, 6:37 PMoffer
you may get into race-conditions, depending on the calling thread (at least it will return false
if it failed). To ensure, use send
.streetsofboston
02/05/2019, 6:38 PMoffer
is kinda like onNext
for Rx. You have to serialize calls to it, otherwise your Subject/Publisher may fail.Allan Wang
02/05/2019, 6:38 PMstreetsofboston
02/05/2019, 6:40 PMoffer
if you don’t mind losing an event here and there…. Or monitor the return value of offer
and implement a sort-of-a retry.Allan Wang
02/05/2019, 6:41 PMspierce7
02/05/2019, 6:59 PMConflatedBroadcastChannel
.
private val broadcastChannel: BroadcastChannel<T> by lazy { ConflatedBroadcastChannel(performGet()) }
If I swap the above to an ArrayBroadcastChannel
, I have no way to always emit the current value to a new subscriber.spierce7
02/05/2019, 6:59 PMAllan Wang
02/05/2019, 7:01 PMspierce7
02/05/2019, 7:09 PMspierce7
02/05/2019, 7:09 PMgildor
02/06/2019, 12:58 AMAllan Wang
02/06/2019, 1:38 AMgildor
02/06/2019, 2:00 AMgildor
02/06/2019, 2:01 AMgildor
02/06/2019, 2:01 AMgildor
02/06/2019, 2:03 AMAllan Wang
02/06/2019, 2:03 AMgildor
02/06/2019, 2:29 AMBut they both will emit the message before subscription if it existsThey emiy nothing if there are no subscribers, just save value
spierce7
02/06/2019, 9:10 PMbehaviorSubject.onNext(1)
behaviorSubject.onNext(2)
behaviorSubject.onNext(3)
behaviorSubject.onNext(4)
I'm guaranteed to receive all 4 events to all subscribers.
If I call
conflatedBroadcastChannel.send(1)
conflatedBroadcastChannel.send(2)
conflatedBroadcastChannel.send(3)
conflatedBroadcastChannel.send(4)
I'm only guaranteed to receive the 4th event on my subscribers. It's very likely that with the above code, I'll actually only receive the 1st and 4th event, and the 2nd and 3rd event will be lost. In fact, that's what I'm seeing consistently in my code running only on my Android main thread.streetsofboston
02/06/2019, 9:13 PMbehaviorSubject
. That largely depends on your observer’s/subscriber’s scheduler (observeOn(scheduler)
) whether you’ll loose some or not.gildor
02/07/2019, 6:17 AMI’m guaranteed to receive all 4 events to all subscribers.@spierce7 It’s not true, exactly what Anton said, it depends also on your schedulers and back pressure strategy. there is no way to guarantee that with BehaviorSubject you get all the updates
spierce7
02/07/2019, 7:09 AMBehaviorSubject
has no backpressure strategy. They don't allow for backpressure. They guarantee all events. I think you are thinking of Publisher, as that is the backpressure enabled form of Subject.gildor
02/07/2019, 7:09 AMsubject.toFlowable(SOME_BACKPRESSURE)
gildor
02/07/2019, 7:10 AMsubject.hide()
, which convert to observable, no backpressure, but also no warranty that you will receive all the eventsgildor
02/07/2019, 7:11 AMThey guarantee all eventsIs there anything in docs about this? If so, why do you need all the events for BehaviorSubject, what is use case of that?
spierce7
02/07/2019, 7:13 AMspierce7
02/07/2019, 7:14 AMspierce7
02/07/2019, 7:15 AMYou have a flow of no more than 1000 elements at its longest: i.e., you have so few elements over time that there is practically no chance for OOME in your application.
https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0#observable-and-flowablegildor
02/07/2019, 7:17 AMspierce7
02/07/2019, 8:36 AMgildor
02/07/2019, 8:42 AMgildor
02/07/2019, 8:43 AM