Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I make the LiveData static?

I don't know if this is a stupid question. This may defeat the purpose of LiveData/ViewModel.

Can I make the LiveData static? My reason is I have a listener from a Service which updates the information. So I need to have a way from a service to "set/change" the LiveData.

I used to do following and it works:
1. Service changes the DB
2. ViewModel listens to the DB change
3. UI updates from the liveData change

I found this way is too slow. To increase the performance, I want something like:
1. Service changes the class object directly
2. ViewModel listens to the the class object changes
3. UI updates from the liveData change

In order to achieve what I want, either I need to make the MutableLiveData static or make the ViewModel class to share the same instance of ViewModel between Activities.

Is this good idea?

public class MyViewModel extends AndroidViewModel {

    // Note: this MutableLiveData is static
    private static MutableLiveData<MyModel> mutableLiveData;

    public MyViewModel(@NonNull Application application) {
        super(application);
    }

    LiveData<MyModel> getLiveDataList() {
        if (mutableLiveData == null) {
            mutableLiveData = new MutableLiveData<>();
            loadDataFromDb();
        }
        return mutableLiveData;
    }

    private void loadDataFromDb() {
        // load data from DB
        // mutableLiveData.setValue(MyModelFromDb); // Omit the real implementation
    }

    // Note: this method is static
    public static void setData(MyModel newData) {
        mutableLiveData.setValue(newData);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
    }
}
like image 747
jclova Avatar asked Nov 08 '22 09:11

jclova


1 Answers

The whole point of ViewModel from Android Jetpack (as opposed to other versions) is for the ViewModel to be lifecycle aware and perform magic like destroying itself when observer is destroyed (activity/fragment), or surviving configuration changes (for example, orientation) without initialising itself all over again thereby making it much easier to deal with issues related to configuration changes.

So if you made the ViewModel or LiveData static you would actually beat their purpose and most likely leak ViewModel's data, though the need to do this is understandable. So this requires you to engineer your way around it, and the first way you mentioned is probably the best way you can do it. I don't understand why you have an issue with the first solution. The way I see it, it provides the best user experience:

  1. You init ViewModel in your fragment or activity in onCreate and add an Observer to the data.
  2. If database already has some data, your observer will receive it instantly and UI will be updated with existing data instantly.
  3. Service makes the API request and changes the DB
  4. DB changes triggers an update to the data in ViewModel
  5. Observer refreshes received data and you pass this to your views/adapters
  6. UI updates with latest data with some nice animations that indicate addition/removal of items.

From what I can see it cant get better than this. Since your question is from months ago, I am curious to know what you ended up doing?

like image 95
Ali Kazi Avatar answered Nov 15 '22 11:11

Ali Kazi