Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update LiveData of a ViewModel from background service and Update UI

Recently I am exploring Android Architecture, that has been introduced recently by google. From the Documentation I have found this:

public class MyViewModel extends ViewModel {     private MutableLiveData<List<User>> users;     public LiveData<List<User>> getUsers() {         if (users == null) {             users = new MutableLiveData<List<Users>>();             loadUsers();         }         return users;     }      private void loadUsers() {         // do async operation to fetch users     } } 

the activity can access this list as follows:

public class MyActivity extends AppCompatActivity {     public void onCreate(Bundle savedInstanceState) {         MyViewModel model = ViewModelProviders.of(this).get(MyViewModel.class);         model.getUsers().observe(this, users -> {             // update UI         });     } } 

My Question is, I am going to do this:

  1. in the loadUsers() function I am fetching the data asynchronously where I will first check the database(Room) for that data

  2. If I do not get the data there I will make an API call to fetch the data from the web server.

  3. I will insert the fetched data into the database(Room) and update the UI according the data.

What is the recommended approach to do this?

If I start a Service to call the API from the loadUsers() method, how can I update the MutableLiveData<List<User>> users variable from that Service?

like image 284
S Haque Avatar asked May 26 '17 15:05

S Haque


People also ask

How UI state is managed with ViewModel?

UI state is usually stored or referenced in ViewModel objects and not activities, so using onSaveInstanceState() requires some boilerplate that the saved state module can handle for you. When using this module, ViewModel objects receive a SavedStateHandle object through its constructor.

Is ViewModel part of UI?

The ViewModel class is designed to store and manage UI-related data in a lifecycle conscious way. The ViewModel class allows data to survive configuration changes such as screen rotations.

What is the purpose of initialising and storing LiveData in the ViewModel instead of a UI controller Multi Choice multi correct?

Why should you initialize and store LiveData in your ViewModel instead of a UI Controller? Both the ViewModel and LiveData are lifecycle aware. To ensure that the LiveData isn't destroyed when the UI Controller is destroyed. To hide or separate implementation details making your app more flexible.

What is the difference between LiveData and MutableLiveData?

By using LiveData we can only observe the data and cannot set the data. MutableLiveData is mutable and is a subclass of LiveData. In MutableLiveData we can observe and set the values using postValue() and setValue() methods (the former being thread-safe) so that we can dispatch values to any live or active observers.


2 Answers

I am assuming that you are using android architecture components. Actually it doesn't matter wherever you are calling service, asynctask or handler to update the data. You can insert the data from the service or from the asynctask using postValue(..) method. Your class would look like this:

private void loadUsers() {     // do async operation to fetch users and use postValue() method     users.postValue(listOfData) } 

As the users is LiveData, Room database is responsible for providing users data wherever it is inserted.

Note: In MVVM like architecture, the repository is mostly responsible for checking and pulling local data and remote data.

like image 154
androidcodehunter Avatar answered Sep 17 '22 04:09

androidcodehunter


You can use MutableLiveData<T>.postValue(T value) method from background thread.

private void loadUsers() {     // do async operation to fetch users and use postValue() method    users.postValue(listOfData) } 
like image 32
minhazur Avatar answered Sep 19 '22 04:09

minhazur