Android @Update level save library not working

I am trying to update my database through the new room library for Android, but it does not work. Here is my approach

@IgnoreExtraProperties @Entity(tableName = CarModel.TABLE_NAME, indices = {@Index(value = "car_name", unique = true)}) public class CarModel { public static final String TABLE_NAME = "cars"; @PrimaryKey(autoGenerate = true) private int id; @ColumnInfo(name = "car_name") private String name; @ColumnInfo(name = "car_price") private String price; private String type; private String position; } 

MainActivity.java

 viewModel.isCarsEmpty().observe(MainActivity.this, new Observer<Integer>() { @Override public void onChanged(@Nullable Integer rowCount) { if (rowCount == 0) { viewModel.insertItems(list); } else { viewModel.updateItems(list); } } }); 

CarViewModel.java

 public LiveData<Integer> isCarsEmpty() { return appDatabase.carDao().isDbEmpty(); } public void insertItems(List<CarModel> carModels) { new insertCarsAsyncTask(appDatabase).execute(carModels); } private class insertCarsAsyncTask extends AsyncTask<List<CarModel>, Void, Void> { private AppDatabase db; public insertCarsAsyncTask(AppDatabase appDatabase) { db = appDatabase; } @Override protected Void doInBackground(List<CarModel>... params) { db.carDao().insertCars(params[0]); return null; } } public void updateItems(List<CarModel> list) { new updateCarsTask(appDatabase).execute(list); } private class updateCarsTask extends AsyncTask<List<CarModel>, Void, Void> { private AppDatabase db; public updateCarsTask(AppDatabase appDatabase) { db = appDatabase; } @Override protected Void doInBackground(List<CarModel>... params) { db.carDao().updateCars(params[0]); return null; } } 

Cardao.java

 @Insert(onConflict = REPLACE) void insertCars(List<CarModel> cars); @Update void updateCars(List<CarModel> param); @Query("SELECT count(*) FROM " + CarModel.TABLE_NAME) LiveData<Integer> isDbEmpty(); 

I debugged, new data appeared and called the viewModel.updateItems (list) method. Thanks in advance!

+11
source share
3 answers

I'm sorry to post this as an answer, but I'm not allowed to add a simple comment, but here is my idea:

Have you tried using only insertCars() instead of updateCars() ? In any case, it looks like your isCarsEmpty() LiveData callback is triggered all the time, because when the observer is called, the database changes again. I'm not quite sure what you want to achieve.

+6
source

Make sure that the row you want to update and the model you submit to update must have the same identifier that you defined as the primary key.

+11
source

The update means that you have a CarModel that is already in the database, and after updating this CarModel you return new values ​​to the database, except for the identifier that you return the same Id

what to do

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

In MainActivity, assign the extracted CarModel to a global variable and use the set functions to update the values ​​of the CarModel member variables.

then pass this global variable to the update function in MainActivity

I have an example on my github like this

0
source

Source: https://habr.com/ru/post/1269827/


All Articles