Android Room persistence library @Update not working Android Room persistence library @Update not working database database

Android Room persistence library @Update not working


Make sure the row you want to updated and model you are sending to update should have same id which you have defined as primary key.


I am sorry for posting this as an answer but I am not allowed to add a simple comment yet so here is my idea:

Have you tried using only insertCars() instead of updateCars()?Anyway it looks like your isCarsEmpty() LiveData callback gets triggered the whole time because when the observer is called the Database is altered again.. I am not quite sure what you want to accomplish tho.


The update means you fetch a CarModel which is already in the database and after updating this CarModel you return the new values to the Database, except the Id you return the same Id

what to do

you need for example a global variable of type CarModel and implement set functions in the CarModel

In MainActivity assignt the fetched CarModel to the global variable and use the set functions to update the values of member variables of the CarModel

then pass this global variable to the update function in MainActivity

I have example in my Github like this