Using Room Database with LiveData | Android Jetpack

Ashish Rawat
Mar 8 · 5 min read

In this article, I am going to talk about Room database with LiveData.

Those who do not know what is Room or has not worked on Room, I have linked my previous blog below

LiveData is an observable data holder, part of the Android Jetpack.

Here is the video from Android Developers Channel:

LiveData is 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 STARTED or RESUMED state. LiveData only notifies active observers about updates. Inactive observers registered to watch LiveData objects aren't notified about changes.

  • Ensures your UI matches your data state
    LiveData follows the observer pattern. LiveData notifies Observer objects when the lifecycle state changes. You can consolidate your code to update the UI in these Observer objects. 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 Lifecycle objects 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 LiveData object using the singleton pattern to wrap system services so that they can be shared in your app. The LiveData object connects to the system service once, and then any observer that needs the resource can just watch the LiveData object. For more information, see Extend LiveData.

Implementation of Room

  1. Open the build.gradle file 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:

We use LiveData when we want to observe an output, so we have the wrap the receiving data with LiveData

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 ofLiveData<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 this will only call when there is any kind of changes in the database.

If you want to see the complete repo


Thanks for reading this article ❤

If I got something wrong? Let me know in the comments. I would love to improve.

Clap 👏 If this article helps you.

Connect with me on LinkedIn.

Check my GitHub repositories.

Follow me on Twitter.

MindOrks

Our community publishes stories worth reading on software development and design. Android | Machine Learning | #MakeEveryoneCode

Ashish Rawat

Written by

Android and Flutter Developer

MindOrks

MindOrks

Our community publishes stories worth reading on software development and design. Android | Machine Learning | #MakeEveryoneCode

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade