Sign in Go Pro

Storing Data in Android with Room

LiveData

This lesson is for PRO members.

Upgrade today to get access to all the PRO lessons.

Unlock this lesson
Autoplay

Up next

Previous

About

In this lesson, we describe what LiveData is and how it can simplify reading values from a Room database.

LiveData is an observable data holder class. LiveData provides a way to run queries asynchronously on a background thread and can notify you automatically whenever a change is made to your database.

We'll demonstrate the power of LiveData by modifying our SELECT query to return LiveData which will always contain the most up-to-date task list from the database.

We'll remove the need to manually refresh the task list; instead, we'll observe the LiveData returned by our SELECT query.

This is moving towards a more reactive architecture, where we have the database as a single source of truth and the UI updating automatically whenever changes occur in the database. This reduces the possibility of the UI and database falling out of sync with each other and reduces the amount of code we need to write.

Summary of Content:

  • Features and advantages of LiveData
  • How to update a SELECT query to return an observable list of tasks
  • How to observe LiveData in order to be notified of any changes
  • LiveData ensures data is read on a background thread, but your observing code is notified on the main thread

Links:

Instructor

Links

Comments

Great lesson.
However, in a typical Android architectural design I would have a ViewModel (or even a layer deeper) thats gets the tasks and does some work on this list or model (sorting, removing all doubles, adding the bitmap for the provided base64 string etc.) and then you cannot use LiveData.

So to me, the only use case to use LiveData in Room is when you don't have to make any changes on the model / List that you request.
Is this assumption correct?

>
You need to go PRO to post comments.

Lessons in Storing Data in Android with Room