【问题标题】:How to convert this method AsyncTask to RxAndroid如何将此方法 AsyncTask 转换为 RxAndroid
【发布时间】:2019-09-16 18:17:39
【问题描述】:

我是 rxAndroid 的新手。我将以下方法与 AsyncTask 一起使用,我想将其转换为 RXjava。

mytask = new AsyncTask<long[], Void, Void>() {
        Void doInBackground(long[] ... longs) {
                for (long user : longs[0]) {
                        if (isCancelled())
                                return null;
                        try {
                                call(user);
                        } catch {
                                //
                        }
                        return null;
                }

                @Override
                protected void onProgressUpdate(Void... voids) {
                        adapter.removeItem(0);
                }
                void onPreExecute() {
                        adapter.setBlocked(true);
                }
                void onPostExecute(Void aVoid) {
                        onStop();
                }

                void onCancelled() {
                        onStop();
                }

                void onStop() {
                        adapter.setBlocked(false);
                }
        }
        .execute(adapter.getRemoveList());
}

我需要使用 RxAndroid 库将此 AsyncTask 转换为 RxJava 的帮助。

【问题讨论】:

    标签: java android


    【解决方案1】:
    Observable.just(input) // your input goes here
                    .map(input -> { //doInBackground with input goes here})
                    .subscribeOn(Schedulers.io())// take cares of doing task in background thread
                    .observeOn(AndroidSchedulers.mainThread())// make sures to deliver result on main thread
                    .doOnSubscribe(() -> {
                         // pre execute logic goes here
                     })
                    .subscribe(_ -> { 
                           //post execute goes here
                     });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-27
      • 2022-01-06
      相关资源
      最近更新 更多