<legend id='4G7yw'><style id='4G7yw'><dir id='4G7yw'><q id='4G7yw'></q></dir></style></legend>

        <bdo id='4G7yw'></bdo><ul id='4G7yw'></ul>

        <small id='4G7yw'></small><noframes id='4G7yw'>

        <tfoot id='4G7yw'></tfoot>

      1. <i id='4G7yw'><tr id='4G7yw'><dt id='4G7yw'><q id='4G7yw'><span id='4G7yw'><b id='4G7yw'><form id='4G7yw'><ins id='4G7yw'></ins><ul id='4G7yw'></ul><sub id='4G7yw'></sub></form><legend id='4G7yw'></legend><bdo id='4G7yw'><pre id='4G7yw'><center id='4G7yw'></center></pre></bdo></b><th id='4G7yw'></th></span></q></dt></tr></i><div id='4G7yw'><tfoot id='4G7yw'></tfoot><dl id='4G7yw'><fieldset id='4G7yw'></fieldset></dl></div>

        Android Room持久性库@Update不起作用

        Android Room persistence library @Update not working(Android Room持久性库@Update不起作用)

          <tbody id='qhwet'></tbody>
          <i id='qhwet'><tr id='qhwet'><dt id='qhwet'><q id='qhwet'><span id='qhwet'><b id='qhwet'><form id='qhwet'><ins id='qhwet'></ins><ul id='qhwet'></ul><sub id='qhwet'></sub></form><legend id='qhwet'></legend><bdo id='qhwet'><pre id='qhwet'><center id='qhwet'></center></pre></bdo></b><th id='qhwet'></th></span></q></dt></tr></i><div id='qhwet'><tfoot id='qhwet'></tfoot><dl id='qhwet'><fieldset id='qhwet'></fieldset></dl></div>
        1. <legend id='qhwet'><style id='qhwet'><dir id='qhwet'><q id='qhwet'></q></dir></style></legend>
            <tfoot id='qhwet'></tfoot>

                • <bdo id='qhwet'></bdo><ul id='qhwet'></ul>

                  <small id='qhwet'></small><noframes id='qhwet'>

                  本文介绍了Android Room持久性库@Update不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

                  I am trying to update my database via new android room library, but it is not working. Here it 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

                  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();
                  

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

                  I did debugging, new data comes and calling viewModel.updateItems(list) method.Thanks in advance!

                  推荐答案

                  我很抱歉发布这个作为答案,但我还不允许添加简单的评论,所以这是我的想法:

                  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:

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

                  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.

                  这篇关于Android Room持久性库@Update不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Cannot solve SonarQube error quot;Make this line start at column 3quot;(无法解决 SonarQube 错误“使此行从第 3 列开始)
                  SonarQube for multi module project for Android(适用于 Android 的多模块项目的 SonarQube)
                  How to integrate sonarqube in android studio?(如何在 android studio 中集成 sonarqube?)
                  Android What is the easist way Upgrade Database for existing DB(Android 为现有数据库升级数据库的简单方法是什么)
                  Updating SQLite database android(更新 SQLite 数据库 android)
                  Android: onUpgrade not calling at database upgrade(Android:onUpgrade 不在数据库升级时调用)

                  <legend id='yR199'><style id='yR199'><dir id='yR199'><q id='yR199'></q></dir></style></legend>
                    <tfoot id='yR199'></tfoot>

                        <tbody id='yR199'></tbody>
                        <i id='yR199'><tr id='yR199'><dt id='yR199'><q id='yR199'><span id='yR199'><b id='yR199'><form id='yR199'><ins id='yR199'></ins><ul id='yR199'></ul><sub id='yR199'></sub></form><legend id='yR199'></legend><bdo id='yR199'><pre id='yR199'><center id='yR199'></center></pre></bdo></b><th id='yR199'></th></span></q></dt></tr></i><div id='yR199'><tfoot id='yR199'></tfoot><dl id='yR199'><fieldset id='yR199'></fieldset></dl></div>

                        <small id='yR199'></small><noframes id='yR199'>

                          <bdo id='yR199'></bdo><ul id='yR199'></ul>