Android Room持久性库@Update无效

时间:2022-08-12 05:33:44

I am trying to update my database via new android room library, but it is not working. Here it is my approach

我试图通过新的android房间库更新我的数据库,但它无法正常工作。这是我的方法

@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

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

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

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 did debugging, new data comes and calling viewModel.updateItems(list) method.Thanks in advance!

我做了调试,新数据来了,并调用viewModel.updateItems(list)方法。提前谢谢!

2 个解决方案

#1


5  

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.

您是否尝试过仅使用insertCars()而不是updateCars()?无论如何,看起来你的isCarsEmpty()LiveData回调一直被触发,因为当调用观察者时,数据库再次被改变..我不太确定你想要实现什么。

#2


3  

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.

确保要更新的行和要发送到更新的模型应具有您已定义为主键的相同ID。

#1


5  

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.

您是否尝试过仅使用insertCars()而不是updateCars()?无论如何,看起来你的isCarsEmpty()LiveData回调一直被触发,因为当调用观察者时,数据库再次被改变..我不太确定你想要实现什么。

#2


3  

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.

确保要更新的行和要发送到更新的模型应具有您已定义为主键的相同ID。