Encapsulate the LiveData

Yudistiro Septian Dwi Saputro
Bootcampers
Published in
3 min readMay 26, 2020

In the development of Android applications using MVVM pattern is one of the best choices in the selection of architectures.

As we know, MVVM is one of the architectural patterns recently introduced by Google Company that will empower you to write manageable, maintainable, cleaner, and testable code. There are too many libraries people use for lifecycle-aware components, LiveData, ViewModel so it depends on your application business which one you choose.

Let’s recap the outline of this architecture

  1. Activity/fragment : just show the result data or information to users, and allow for interaction, so in the view, we can't add logic or processing data.
  2. ViewModel : Bridge between the view and model, Exposes business logic relevant to a specific view
  3. Repository : Responsible For processing data, like deleting data, saving data, or getting data from API,

if you are not familiar with MVVM I recommend you read this article

But when I learn first time to use MVVM, I confusion why in ViewModel we must make variables with MutableLiveData and LiveData like this

private val _data = MutableLiveData<Response>()
private val _isLoading = MutableLiveData<Boolean>()
private val _onError = MutableLiveData<String>()
private val _onNetworkError = MutableLiveData<Boolean>()

val dataResp: LiveData<Response> get() = _data
val isViewLoading: LiveData<Boolean> get() = _isLoading
val onError: LiveData<String> get() = _onError
val onNetworkError: LiveData<Boolean> get() = _onNetworkError

So I search many articles to understand why we must use it, and I got articles that compares livedata and mutablelivedata.

MutableLiveData vs. LiveData:

  • Data in a MutableLiveData object can be changed, as the name implies. Inside the ViewModel, the data should be editable, so it uses MutableLiveData.
  • Data in a LiveData object can be read, but not changed. From outside the ViewModel, data should be readable, but not editable, so the data should be exposed as LiveData.

Then i understand what I mistake, the Big Idea is to encapsulation data from other class, especially from activity/fragment.

But what meaning is encapsulation data?

Encapsulation is a way to restrict direct access to some of an object’s fields. When you encapsulate an object, you expose a set of public methods that modify the private internal fields. Using encapsulation, you control how other classes manipulate these internal fields.

As I said in point 1 in the outline of MVVM, views cannot deal with data or logic.

Let’s back to code, when we create a variable with Mutablelivedata we must add _ in front of the name because it follows the naming convention of backing property.

private val _data = MutableLiveData<Response>()

If you need to access a field of a property inside a class in which it’s declared, you can use backing properties. If you only need to access a field from the getter or setter of its property, it’s enough to use backing fields. However, if you want to access a field somewhere else, you should use backing properties.

Then we can access value from _data in Livedata with get() method when we declare it,

val dataResp: LiveData<Response> get() = _data

get() method is like getter to get data from _data. Now we observable dataResp in view or data binding with Encapsulate data, so the data save from other classes.

I hope you can understand what I explained Lol,

Link Reference

--

--