https://kotlinlang.org logo
#coroutines
Title
# coroutines
s

svenjacobs

08/28/2019, 7:30 AM
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

bdawg.io

08/28/2019, 7:36 AM
Yeah, those UI events are hot since you want the click event itself and the handler to be fully decoupled but listening.
s

svenjacobs

08/28/2019, 7:37 AM
Thanks Bradyn, that's what I tought
d

darkmoon_uk

08/28/2019, 7:37 AM
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

svenjacobs

08/28/2019, 7:40 AM
Thanks Chris, I'm already using
ConflatedBroadcastChannel
🙂 So basically channels are the right choice for hot UI events
👍 1
d

darkmoon_uk

08/28/2019, 7:40 AM
Yep
s

svenjacobs

08/28/2019, 7:40 AM
But I might replace
Channel
with
Flow
for my API requests
d

darkmoon_uk

08/28/2019, 7:41 AM
Yep, or note you can use
asFlow
to gain the Flow-API benefits for any
BroadcastChannel
.
👍🏼 1
I'm waiting for this 🙂
e

elizarov

08/28/2019, 8:57 AM
d

darkmoon_uk

08/28/2019, 9:19 AM
@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

Dominaezzz

08/28/2019, 1:28 PM
Use
callbackFlow
.
👍 1
3 Views