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:
in the loadUsers()
function I am fetching the data asynchronously where I will first check the database(Room) for that data
If I do not get the data there I will make an API call to fetch the data from the web server.
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
?
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.
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.
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.
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.
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.
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) }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With