Using Room DataBase with LiveData | Android Jetpack
LiveData is an observable data holder, part of the Android Jetpack and we are using live data to observe the data that is we are retrieving from the database and if you do not know what is Room, we have linked my previous blog below
LiveDatais an observable data holder class. Unlike a regular observable, LiveData is lifecycle-aware, meaning it respects the lifecycle of other app components, such as activities, fragments, or services. This awareness ensures LiveData only updates app component observers that are in an active lifecycle state.
LiveData considers an observer, which is represented by the
Observer class, to be in an active state if its lifecycle is in the
RESUMED state. LiveData only notifies active observers about updates. Inactive observers registered to watch
LiveData objects aren’t notified about changes.
Why use LiveData?
- Ensures your UI matches your data state
LiveData follows the observer pattern. LiveData notifies
Observerobjects when the lifecycle state changes. You can consolidate your code to update the UI in these
Observerobjects. Instead of updating the UI every time the app data changes, your observer can update the UI every time there’s a change.
- No memory leaks
Observers are bound to
Lifecycleobjects and clean up after themselves when their associated lifecycle is destroyed.
- No crashes due to stopped activities
If the observer’s lifecycle is inactive, such as in the case of an activity in the back stack, then it doesn’t receive any LiveData events.
- No more manual lifecycle handling
UI components just observe relevant data and don’t stop or resume observation. LiveData automatically manages all of this since it’s aware of the relevant lifecycle status changes while observing.
- Always up to date data
If a lifecycle becomes inactive, it receives the latest data upon becoming active again. For example, an activity that was in the background receives the latest data right after it returns to the foreground.
- Proper configuration changes
If an activity or fragment is recreated due to a configuration change, like device rotation, it immediately receives the latest available data.
- Sharing resources
You can extend a
LiveDataobject using the singleton pattern to wrap system services so that they can be shared in your app. The
LiveDataobject connects to the system service once, and then any observer that needs the resource can just watch the
LiveDataobject. For more information, see Extend LiveData.
Implementation of Room
Step 1: Add the Gradle dependencies
- Open the
build.gradlefile for your project (not the ones for your app or module) and add the
google()repository as shown below:
2. Open the
build.gradle file for your app or module and add dependencies:
Step 2: Add LiveData
We use LiveData when we want to observe an output, so we will wrap the receiving data with LiveData so we can observe the data retrieving from the database.
Let’s see How we do it.
First of all, we need to identify where we want to put the observer, In our case, we will observe the data that is retrieving from the database.
Before adding LiveData we can see we are just retrieving the List of persons from the database.
When we add LiveData we just need to wrap with LiveData so we can attach an observer.
Now we will receive the object of
LiveData<List<Person>> instead of
List<Person>and you will see the error where you are calling the
loadAllpersons() method from DAO.
loadAllpersons() method is now returning a LiveData Objects from DAO.
So Instead of doing this 👇
You will do this
Now we have attached an observer to
loadAllpersons() so we can observe the changes in the database.
This observer will only observe when there is any change in the database.
Now do not have to worry about where you gonna call this because
onChangedmethod will only call when there are any kind of changes in the database.
We have also linked a repo if you need to see the example of Room Database.
Room Database with LiveData Demo (Source Code).
That’s all for Room Database.
This is a series for Android Jetpack, we will be back with other Android Jetpack Components.
Thanks for reading this article ❤
If we got something wrong? Let me in the comments. we would love to improve.