In this lesson, we describe what
LiveData is and how it can simplify reading values from a
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
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
- How to update a SELECT query to return an observable list of tasks
- How to observe
LiveDatain order to be notified of any changes
LiveDataensures data is read on a background thread, but your observing code is notified on the main thread
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?