使用RxJava在主线程室中执行delete

时间:2018-01-13 05:25:17

标签: android rx-java android-room

我使用的房间用于存储有关购物车详情的信息。

我想detele主线程中的记录。我收到错误

  

无法访问主线程上的数据库,因为它可能会长时间锁定

我检查了以下链接,但是dint帮助我

executing delete with room (rxjava)

How to insert data and get id as out parameter in android room and rxjava 2?

DaoAccess.java

    @Dao
    public interface DaoAccess {

        @Insert
        void insertMultipleRecord(DBProducts... universities);

        @Insert
        void insertMultipleListRecord(List<DBProducts> universities);

        @Insert
        void insertOnlySingleRecord(DBProducts university);


        @Query("SELECT * FROM DBProducts")
        LiveData<List<DBProducts>> fetchAllData();


        @Update
        void updateRecord(DBProducts university);

        @Delete
        void deleteRecord(DBProducts university);
    }

Application.class

    public class PRApp extends Application {
        private static PRApp m_instance;
        DBHelper dbHelper;

        @Override
        public void onCreate() {
            super.onCreate();
            mInstance = this;
            prefs = new PRPrefs(this);
            m_instance = this;

            sDefSystemLanguage = Locale.getDefault().getLanguage();
            switch (sDefSystemLanguage) {
                case "en":
                    sDefSystemLanguageCode = "1";
                    break;
                case "ar":
                    sDefSystemLanguageCode = "2";
                    break;
                default:
                    sDefSystemLanguageCode = "3";
                    break;
            }
        }


        public DBHelper getRoomDB() {
            dbHelper = Room.databaseBuilder(getApplicationContext(),
                    DBHelper.class, "prasukh-db").build();
            return dbHelper;

        }



    }

我的异步任务正在工作

    new AsyncTask<Void, Void, Integer>() {
                    @Override
                    protected Integer doInBackground(Void... params) {
                        PRApp.getInstance().getRoomDB().daoAccess().deleteRecord(products.get(position));
                        return 0;
                    }

                    @Override
                    protected void onPostExecute(Integer agentsCount) {
                        Utilities.decrementBadegeCount();
                        productsArrayList.remove(position);
                        cardAdapter.notifyDataSetChanged();
                        notifyDataSetChanged();
                        hideProgressDialog();
                        setCartLayout();
                    }
                }.execute();

如何在RxJava中实现这一点。因为DAO delete返回void

1 个答案:

答案 0 :(得分:2)

使用fromCallable运算符。

Observable.fromCallable(() -> {
    PRApp.getInstance().getRoomDB().daoAccess().deleteRecord(products.get(position));
    return true;
}).firstOrError()
     .subscribeOn(Schedulers.io())
     .observeOn(AndroidSchedulers.mainThread())
     .subscribe(new SingleObserver<List<String>>() {

          @Override
          public void onSubscribe(Disposable d) {

          }

          @Override
          public void onSuccess(List<String> strings) {

          }

          @Override
          public void onError(Throwable e) {

          }

      });
      // or only
      .subscribe()
      // But if an error occurred  in delete operation the app will crash

执行删除操作并生成Observable<Boolean>

或者您可以使用

允许在主线程中访问数据库
Room.databaseBuilder(getApplicationContext(),DBHelper.class, "prasukh-db")
        .allowMainThreadQueries()
        .build();

但由于性能的原因,这是不鼓励的。