Room : LiveData from Dao will trigger Observer.onChanged on every Update, even if the LiveData value has no change

There is simple solution in Transformations method distinctUntilChanged.expose new data only if data was changed. In this case we get data only when it changes in source: LiveData<YourType> getData(){ return Transformations.distinctUntilChanged(LiveData<YourType> source)); } But for Event cases is better to use this: https://stackoverflow.com/a/55212795/9381524

How to save enum field in the database room?

You can make a convert to each enum, like this: class Converters { @TypeConverter fun toHealth(value: String) = enumValueOf<Health>(value) @TypeConverter fun fromHealth(value: Health) = value.name } Or if you prefer store it as SQL integer, you can use ordinal too: class Converters { @TypeConverter fun toHealth(value: Int) = enumValues<Health>()[value] @TypeConverter fun fromHealth(value: Health) = value.ordinal … Read more

LiveData remove Observer after first callback

There is a more convenient solution for Kotlin with extensions: fun <T> LiveData<T>.observeOnce(lifecycleOwner: LifecycleOwner, observer: Observer<T>) { observe(lifecycleOwner, object : Observer<T> { override fun onChanged(t: T?) { observer.onChanged(t) removeObserver(this) } }) } This extension permit us to do that: liveData.observeOnce(this, Observer<Password> { if (it != null) { // do something } }) So to answer … Read more

Room – LiveData observer does not trigger when database is updated

I had a similar problem using Dagger 2 that was caused by having different instances of the Dao, one for updating/inserting data, and a different instance providing the LiveData for observing. Once I configured Dagger to manage a singleton instance of the Dao, then I could insert data in the background (in my case in … Read more

Return type for Android Room joins

Dao @Query(“SELECT * FROM Foo”) List<FooAndBar> findAllFooAndBar(); Class FooAndBar public class FooAndBar { @Embedded Foo foo; @Relation(parentColumn = “Foo.bar_id”, entityColumn = “Bar.id”) List<Bar> bar; // If we are sure it returns only one entry // Bar bar; //Getter and setter… } This solution seems to work, but I’m not very proud of it. What do … Read more

How to dynamically query the room database at runtime?

Room supports @RawQuery annotation to construct queries at run-time. Step 1 : Make DAO method Mark the DAO method with @RawQuery annotation instead of normal @Query. @Dao interface BooksDao{ @RawQuery List<Book> getBooks(SupportSQLiteQuery query); } Step 2 : Construct the query Room uses prepared statements for security and compile time verification. Therefore, while constructing queries, we … Read more

How to use Room Persistence Library with pre-populated database?

This is how I solved it, and how you can ship your application with a pre-populated database (up to Room v. alpha5) put your SQLite DB database_name.db into the assets/databases folder take the files from this repo and put them in a package called i.e. sqlAsset in your AppDatabase class, modify your Room’s DB creation … Read more