【问题标题】:Executing delete in main thread room using RxJava使用 RxJava 在主线程房间执行删除
【发布时间】:2018-01-13 05:25:17
【问题描述】:

我正在使用 Room 来存储有关购物车详细信息的信息。

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

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

我检查了以下链接,但请帮帮我

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

应用程序类

    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

【问题讨论】:

    标签: android rx-java android-room


    【解决方案1】:

    使用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&lt;Boolean&gt;

    或者你可以允许使用主线程访问数据库

    Room.databaseBuilder(getApplicationContext(),DBHelper.class, "prasukh-db")
            .allowMainThreadQueries()
            .build();
    

    但由于性能原因,不鼓励这样做。

    【讨论】:

    • 我试过 Observable.fromCallable(() -> { PRApp.getInstance().getRoomDB().daoAccess().deleteRecord(products.get(position)); return true; }) 不工作?
    • @R2R 你订阅了吗? fromCallable 仅在您订阅时执行。
    • 我是 rxjava 新手,所以你能分享一下如何订阅它
    • @R2R 我编辑了答案。如果你有问题问。乐于助人。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多