我是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的帮助。

最佳答案

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

10-08 08:51