Search code examples
android-roomobserver-patternandroid-livedata

Observe the change of selected attributes of objects in LiveData


I am observing following LiveData:

BookDao:

@Query("SELECT * FROM books")
LiveData<List<Book>> getBooks();

In fragment, I am observing this way (simplified):

viewModel.getBooks().observe(getViewLifecycleOwner(), b -> adapter.setBooks(b));

Everything is fine, adapter displays all books. However, I need to disable refresh when a flag changes in db (favourite column). In other words, when any book is marked as favourite, I do not want observer to run. Is there any way how to observe all columns of book except for favourite column? Adding condition to observer is not a good idea, since comparing new state with previous state leads to O(n^2) complexity. Thank you.


Solution

  • In other words, when any book is marked as favourite, I do not want observer to run.

    You can use .removeObserver() after you get the first query.

    Is there any way how to observe all columns of book except for favourite column?

    Write a sql query in Dao to do that, then observe it, like:

    SELECT Title, Body FROM TABLENAME;