Fun with LiveData (Android Dev Summit '18)
Why Stack Overflow recommends it
Stack Overflow users link it when answering LiveData questions about switchMap and Transformations, MediatorLiveData, observers not firing, and exposing only immutable LiveData from a ViewModel.
Recommended by 5 people across 4 different months, 2018 to 2020.
What it is
An Android Dev Summit 2018 talk by Jose Alc\u00e9rreca and Yigit Boyar on using LiveData beyond the basic ViewModel case. It covers map, switchMap and MediatorLiveData transformations, handling events, building reactive architectures, and common anti-patterns and code smells.
What people said
Recommends Pavan Varma · 2018-11 · Stack Overflow · on “ViewModel manipulation in MVVM Architecture”
Here i have used Transformations.switchMap() [LINK] and Transformations.map() [LINK] For detail understanding of Transformations and MediatorLiveData, you can watch this Video from Android Dev Summit '18 - Fun with LiveData [this video] private MutableLiveData<Integer> boxNumberLiveData = new MutableLiveData<>(); private final LiveData<List<Karteikarte>> allCardsLiveData; //Observe this from Fragment or Activity public final LiveData<Karteikarte> karteikarteLiveData; LearningViewModel(){ // when ever you change box number below function is called and list of Karteikarte will be updated allCardsLiveData = Transformations.switchMap(boxNumberLiveData, new Function<Integer, LiveData<List<Karteikarte>
Recommends Drig drigov · 2018-11 · Stack Overflow · on “Will the `onChanged` method trigger just as we change the data? Even if we dont change the state of activity?”
Google actually described [this video] that (or very familiar to that) kind of issue as an anti-pattern on the dev-summit. The recommended approach is to use switchMap from android.arch.lifecycle.Transformations class. You can see the documentation page [LINK] for more details.
Recommends Andy Cass · 2019-08 · Stack Overflow · on “LiveData keeps on outputting changes to last given observable”
If anyone is having similar issues this may be of some use, Rather than unsubscribing from the old Live Data the Live Data must be changed upstream and the changes will propagate down, this is done using Transforms as per the Android developers best practices [this video] mViewModel.setMutableLiveItem(id); mViewModel.getMutableLiveItem()).observe(this, song -> mDisposable.add(Observable.just(song) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(mSong -> { Log.d(TAG, "filtered " + mSong.toString()); } }))); } Then in The View Model private val mItemId = MutableLiveData<String>() fun setMutableLiveItem(mId: String) { this.mItemId.value = mId } fun getMutableLiveI
Recommends user12194200 · 2019-10 · Stack Overflow · on “Observer for MutableLiveData not being triggered”
I'm not 100% sure, but I believe this video [this video] from the Android team talks about the issue you're facing.
Recommends Karan Dhillon · 2020-04 · Stack Overflow · on “View not being updated using LiveData, a TimerTask and Retrofit”
What you want is your dataSet which is being fetched to the recyclerView's Adapter to observe the LiveData. If you want to do some modifications on the LiveData then use Transformations on it. What you are doing right now is getting the liveData, modifying it in your addElements() method. Instead do that modifications in the ViewModel. Your Activity should only be used as container. Watch this video and you will understand [this video]
Cites Mitesh Vanaliya · 2018-12 · Stack Overflow · on “Android Architecture Components(MVVM) - Ideal way to deal with remote and local data using repository pattern”
Two way to communication between viewmodel and repository is Using callback interface Using Transformations. Transformations has map and switchMap operators similar to RxJava which can converts on livedata to different one You can also use Mediator livedata to create your own custom operators. MediatorLiveData is used for observing multiple livedata sources and perform on there changes. For more information about MVVVM and live data [this video] Medium post [LINK]
Cites EpicPandaForce · 2019-03 · Stack Overflow · on “Calculation Problem in Helper Class - Return is hit before calculation”
public BigDecimal calculateMonthlyCosts(SubViewModel subViewModel...) { If you are using LiveData, then your calculations should not immediate in the form of synchronous function. You don't want to return a BigDecimal, you want to create a LiveData that is based on chain between your other LiveData that will update the BigDecimal when either of the LiveData changes. For this, you should create a single MediatorLiveData, and should observe that LiveData to receive the latest value of the BigDecimal whenever any of its "components" change. public LiveData<BigDecimal> calculateMonthlyCosts(SubViewModel subViewModel) { MediatorLiveData<BigDecimal> mediator = new MediatorLiveData<>() { private Bi
Cites adamhurwitz.eth · 2019-04 · Stack Overflow · on “Android - Best Practices for ViewModel State in MVVM?”
@EtienneLenhart, It is best practice to only expose immutable `LiveData` objects as opposed to `MutableLiveData` publicly because the values may be unintentionally altered outside of the **ViewModel**. The Android team recommends this best practice as described in the 2018 Android Developer Summit talk Fun with LiveData [this video] .