I am using Live Data to publish states from View Model to Fragments, this might result in states getting published frequently. But the Mutable Live Data is skipping the initial values and taking the latest value available.
There is an article which talks about this characteristic, but is there a way of handling this case, such as Flowable in RxJava or setting Back Pressure Strategy or will I need to go back to using RxJava and handle Life-cycle based publishing?
Following is a sample code which shows this behaviour. Values from 1 to 10 are published but only two values are received, 0 and 10. Can we change this behaviour in Live Data or should I use RxJava for this purpose?
Fragment (Subscriber) :
class ParentFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
viewModel = ViewModelProviders.of(
this, ParentViewModelFactory(this, null)
).get(ParentViewModel::class.java)
viewModel.fastLiveData.observe(this, Observer {
Timber.i(it.toString())
})
viewModel.startPublishing()
}
}
View Model (Publisher):
class ParentViewModel(private val savedState : SavedStateHandle)
: ViewModel<ParentState>() {
val fastLiveData : MutableLiveData<Int> = MutableLiveData(0)
fun startPublishing() {
for(x in 1..10) {
Timber.i(x.toString())
fastLiveData.postValue(x)
}
}
}
Output :
(ParentViewModel.kt:30)#startPublishing: 1
(ParentViewModel.kt:30)#startPublishing: 2
(ParentViewModel.kt:30)#startPublishing: 3
(ParentViewModel.kt:30)#startPublishing: 4
(ParentViewModel.kt:30)#startPublishing: 5
(ParentViewModel.kt:30)#startPublishing: 6
(ParentViewModel.kt:30)#startPublishing: 7
(ParentViewModel.kt:30)#startPublishing: 8
(ParentViewModel.kt:30)#startPublishing: 9
(ParentViewModel.kt:30)#startPublishing: 10
(ParentFragment.kt:57)#onChanged: 0
(ParentFragment.kt:57)#onChanged: 10
postValue
can be used on background, and value will be set on main thread asynchronously.setValue
will set value immediately on current thread but current thread should be main thread. – Salol