Orhan Tozan
03/18/2020, 6:56 PMclass ViewModel {
val selectedColor: Flow<Color> = ...
fun onNewColorSelect(newColor: Color) {
...
}
}
How can the onNewColorSelect
method update the selectedColor
flow? With Android, you would use MutableLiveData
. How would you solve this problem with Flow?zak.taccardi
03/18/2020, 6:56 PMval selectedColorChannel = ConflatedBroadcastChannel<Color>(..)
val selectedColor: Flow<Color> = selectedColorChannel.asFlow()
Orhan Tozan
03/18/2020, 6:57 PMzak.taccardi
03/18/2020, 6:58 PMselectedColorChannel.offer(color)
zak.taccardi
03/18/2020, 6:59 PMFlow<T>
with a ConflatedBroadcastChannel<T>
, at least until StateFlow
is implemented https://github.com/Kotlin/kotlinx.coroutines/issues/1082zak.taccardi
03/18/2020, 7:00 PMChannel<T>
is the synchronization primitive that allows you to communicate between coroutinesOrhan Tozan
03/18/2020, 7:01 PMOrhan Tozan
03/18/2020, 7:01 PMzak.taccardi
03/18/2020, 7:02 PMT
zak.taccardi
03/18/2020, 7:02 PMzak.taccardi
03/18/2020, 7:03 PMChannel<T>
is like passing around a List. It’s not an abstraction over observing values of T
Orhan Tozan
03/18/2020, 7:04 PMConflatedBroadcastChannel<T>
is like passing around the latest value of T
?.zak.taccardi
03/18/2020, 7:04 PMFlow<T>
to a ConflatedBroadcastChannel<T>
, this abstraction allows you to register a call to .openSubscrition()
, and when the Flow is closed, close the ReceiveChannel<T>
you get from that .openSubscription()
callzak.taccardi
03/18/2020, 7:06 PMConflatedBroadcastChannel<T>
is literally a reactive version of a regular variable like var state: T
Orhan Tozan
03/18/2020, 7:07 PMOrhan Tozan
03/18/2020, 7:07 PMChannel<T>
and BroadcastChannel
?Orhan Tozan
03/18/2020, 7:09 PMDoes that mean people should generally useis literally a reactive version of a regular variable likeConflatedBroadcastChannel<T>
var state: T
ConflatedBroadcastChannel<T>
over Flow<T>
with ViewModels?zak.taccardi
03/24/2020, 6:05 PMDoes that mean people should generally use ConflatedBroadcastChannel<T> over Flow<T> with ViewModels? (edited)You actually use both
zak.taccardi
03/24/2020, 6:08 PM