Search code examples
androidperformanceviewmodelandroid-architecture-componentsandroid-viewmodel

What is the use of return statement when using with LiveData


I have followed this example, an integrated ViewModel and LiveData.

I used LiveData for ViewModel to Repository communication, and activity to ViewModel communication

I have few confusions that I want to clear this question.

This is working fine, and display Toast Message after 5 seconds on MainActivity.

MainActivity

    MainViewModel homeViewModel = ViewModelProviders.of(this).get(MainViewModel.class);

    homeViewModel.getResponseval().observe(this, new Observer<String>() {
        @Override
        public void onChanged(@Nullable String s) {
            Toast.makeText(getApplicationContext(), "MainActivityObserverCalled", Toast.LENGTH_LONG).show();
        }
    });

MainViewModel

public class MainViewModel extends ViewModel {

    public MutableLiveData<String> responseval;
    private LoginRepositry loginRepositry;

    public MainViewModel(){
        loginRepositry = new LoginRepositry();
        responseval = loginRepositry.getData("username","password");
    }


    public MutableLiveData<String> getResponseval() {
        return responseval;
    }       

LoginRepositry

public class LoginRepositry {

    private MutableLiveData<String> data = new MutableLiveData<>();

    public  MutableLiveData<String> getData(String username , String userpass) {

        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                data.setValue("Login Repo Set Value");

            }
        }, 5000);

        return data;
    }

These are my 2 questions.

  • Now with each method, I am returning some data of type LiveData, but at the time of returning the data, the value is not set. I set it after 5 seconds data.setValue("SomeValue"), So what is the use of return here, is it only because of method return type, and does nothing in case of LiveData

  • In MainActivity, i used homeViewModel.getResponseval().observe to observer data, but in ViewModel, I didn't use observe, but ViewModel is still observing the Repository and after 5 seconds MainActivity gets a result. I am unable to understand whats happening here .


Solution

  • So let's do this by parts:

    Now with each method i am returning some data of type LiveData, but at the time of returning the data, the value is not set. I set it after 5 seconds data.setValue("SomeValue"), So what is the use of return here, is it only because of method return type, and does nothing in case of LiveData

    If you check the docs you'll see that LiveData "is an observable data holder class", so it holds data and you can observer it. This is very important to understand why you're returning this LiveData object here. By returning it you're telling the next layer of your architecture (the ViewModel) "hey, here is this holder, I will put some data here at some point, so observer it if you want to receive the data".

    The ViewModel doesn't observe it, but simply pass it to the next entity interested in this holder of data, the UI (LifeCycleOwner). So in the owner you start to observe this "holder of data" and will be warned when new data arrives.

    In MainActivity, i used homeViewModel.getResponseval().observe to observer data, but in ViewModel, I didn't use observe, but ViewModel is still observing the Repository, and after 5 seconds MainActivity gets result. I am unable to understand whats happening here.

    You need a LifeCycleOwner in order to observe updates from a LiveData, also from the docs: "LiveData considers an observer, which is represented by the Observer class, to be in an active state if its lifecycle is in the STARTED or RESUMED state. LiveData only notifies active observers about updates." In order to detect the state, it needs a LifeCycleOwner, that's why when you have the observe method you pass this as a parameter. And that's why you didn't use observe in the ViewModel.