Hi all, I'm currently trying to replace usages of ...
# coroutines
s
Hi all, I'm currently trying to replace usages of
Channel
with
Flow
in my Android application. But now I'm stuck in a conceptual thinking problem: Right now I use
Channel
to handle UI events on Android views, for instance clicks inside a
RecyclerView.Adapter
. I create an instance of
Channel
in the view (or presenter, it doesn't matter) and pass it to the
RecyclerView.Adapter
. Then inside the adapter I could just pass click events to the channel. But with
Flow
, the flow needs to be created at the place where the data is emitted. However I don't want to have x flows in my Adapter for every view the adapter manages. Is
Flow
even suited for UI events or are UI events classically hot streams? Am I just trying to apply
Flow
where
Channel
is the right choice after all?
b
Yeah, those UI events are hot since you want the click event itself and the handler to be fully decoupled but listening.
s
Thanks Bradyn, that's what I tought
d
Flow
isn't necessarily a replacement for
Channel
-
Channel
is appropriate for 'hot' events like live interactions with your UI.
If you've come from experience with RxJava, then you might be used to working with the hybrid behaviour of a
BehaviourSubject
- subscription to which offers an immediate 'last known' value (cold), followed by subsequent updates (hot).
There is a coroutines equivalent to this model - the
ConflatedBroadcastChannel
...pretty handy for UI work.
s
Thanks Chris, I'm already using
ConflatedBroadcastChannel
🙂 So basically channels are the right choice for hot UI events
👍 1
d
Yep
s
But I might replace
Channel
with
Flow
for my API requests
d
Yep, or note you can use
asFlow
to gain the Flow-API benefits for any
BroadcastChannel
.
👍🏼 1
I'm waiting for this 🙂
e
d
@elizarov
StateFlow
is most incisive, because: - Data Flow implies data in an untyped, raw form, which this is (usually) not. - Value Flow somewhat clashes by name with Kotlins
val
, implying unchanging, or constant, which this is not. - State Flow - as you said yourself, succinctly point to the fact there is an ever-present state of this thing, which can be accessed... but which flows, changes.
d
Use
callbackFlow
.
👍 1