Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Viewmodel observing LiveData - how?

Before Lifecycle and LiveData stuff, Viewmodel could observe changes to its own observables quite easily. It was only a matter of subscribing to Observable*'s changes and react to them. Then one might used a two-way binding to react immediately on user's input.

In Android Studio Canary, it is now allowed to bind to LiveData objects, provided that Binding knows lifecycle of its owner (ViewBinding now has additional setLifecycle method), by Android Studio Canary information:

You can now use a LiveData object as an observable field in data binding expressions. The ViewDataBinding class now includes a new setLifecycle method that you need to use to use to observe LiveData objects.

However, Viewmodel documentation states clearly:

ViewModel objects can contain LifecycleObservers, such as LiveData objects. However ViewModel objects must never observe changes to lifecycle-aware observables, such as LiveData objects.

(emphasis mine)

So how one can react immediately to LiveData changes if ViewModel cannot subscribe to them?

And, furthermore, why Viewmodel can not observe changes to its own LiveData?

like image 940
Spook Avatar asked Jan 04 '18 10:01

Spook


People also ask

How do you observe LiveData in ViewModel?

Attach the Observer object to the LiveData object using the observe() method. The observe() method takes a LifecycleOwner object. This subscribes the Observer object to the LiveData object so that it is notified of changes. You usually attach the Observer object in a UI controller, such as an activity or fragment.

Can we observe in ViewModel?

ViewModel allows the app's data to survive configuration changes. In this codelab, you'll learn how to integrate LiveData with the data in the ViewModel . The LiveData class is also part of the Android Architecture Components and is a data holder class that can be observed.

How does a ViewModel work internally?

ViewModel class is designed to store and manage UI-related data in a lifecycle conscious way.It is the main component in the MVVM architecture. ViewModel can be created with activity context or fragment context. When a ViewModel object is created, it is stored inside Activity OR FragmentManager.

What is difference between ViewModel and LiveData?

ViewModel : Provides data to the UI and acts as a communication center between the Repository and the UI. Hides the backend from the UI. ViewModel instances survive device configuration changes. LiveData : A data holder class that follows the observer pattern, which means that it can be observed.


1 Answers

I fixed this issue using MediatorLiveData. You can follow the steps below.

  1. Create a variable (ex. result).

    private final MediatorLiveData<Resource<RequestType>> result = new MediatorLiveData<>();
    
  2. Call addSource method where the first argument is LiveData and the second is an observer.

    LiveData<Resource<EmptyResponse>> observable = service.createItems();
    result.addSource(observable, response -> {
        //Write code here 
    }
    

Also see this SO answer. The basic requirement is almost the same.

like image 124
Saurabh Khare Avatar answered Oct 07 '22 00:10

Saurabh Khare