Remy Benza
02/06/2020, 8:00 AMMark McCormick
02/06/2020, 8:40 AMtseisel
02/06/2020, 8:43 AMFlow
has way more operators.
Also, LiveData
is only consumed from the Main thread, while Flow
could be consumed from any context (using flowOn
operator).
But LiveData
has the advantage of saving the latest received value (liveData.getValue()
).Remy Benza
02/06/2020, 11:13 AMShmuel Rosansky [G]
02/06/2020, 12:01 PMMark Murphy
02/06/2020, 12:12 PMasLiveData()
extension function on Flow
to wrap it in a LiveData
. So, your diagram a couple of comments earlier in this thread is fairly easy to implement.Remy Benza
02/06/2020, 12:19 PMstreetsofboston
02/06/2020, 12:24 PMBrian
02/06/2020, 12:58 PMAnd this green outlined text kind of suprised me. Observe Livedata in your viewmodel from your Activity or Fragment is like super standard?i think the text in your screenshot happens to be awkwardly worded, what it means is don’t have your ViewModel observe a LiveData property, observing a LiveData property in your ViewModel with an Activity or Fragment is super standard, as you say
Remy Benza
02/06/2020, 1:00 PMWe use LiveData only as properties in ViewModels for Activities/Fragments to observe. All the other stream types are either Flows or Rx Observables. The Flow.asLiveData() has some issues with its pausing dispatcher and we don't yet use it.But would you consider Livedata a 'steam' type? Since it returns a whole list like 'listOf(1, 2, 3)' instead of a flow of items in the list '1, 2, and 3'.
Mark Murphy
02/06/2020, 1:02 PMFlow
can emit List<Int>
and LiveData
can emit Int
. The type of what is being emitted to observers is independent of whether it is Flow
or LiveData
.Remy Benza
02/06/2020, 1:02 PMbdawg.io
02/06/2020, 4:55 PMzak.taccardi
02/06/2020, 5:30 PMLiveData<T>
anywhere except in a ViewModel
to expose a T
to a UI component (such as an Activity
or Fragment
). Flow<T>
should be preferred everywhere else (and I prefer it at the ViewModel
level too because it avoids the main thread limitation)LiveData<T>
is analogous to a Flow<T>
backed by a ConflatedBroadcastChannel<T>
(or a StateFlow<T>
in the next release of Kotlin Coroutines).
This means that the T
is replayed to new subscribers on initial subscriptionLiveData<T>
has a specific behavior (replaying latest value on subscription) that certain implementations Flow<T>
support. Flow<T>
is the more flexible type, which can cause some confusion because how the T
emits from can differ from Flow<T>
to Flow<T>