0

I have a situation where I want to load some data to populate a recyclerview using LiveData and ViewModel. Something like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    ...
    viewModel.loadData().observe(this, data -> {
        adapter = new AppsListAdapter(data);
        recyclerView.setAdapter(adapter);;
    });
}

What loadData() basically does is it returns a MutableLiveData object wrapping the data.

Now here is the problem, the LiveData observer triggers the callback every time the data changes. Meaning that every time the data changes, a new instance of the adapter is created which is not very efficient. Is there anyway I can remove the observer after the first callback call? I hope my question was clear and thanks.

Toni Joe
  • 7,715
  • 12
  • 50
  • 69

0 Answers0