【问题标题】:How do I convert my asynctask to Rxjava?如何将我的 asynctask 转换为 Rxjava?
【发布时间】:2017-12-11 20:35:20
【问题描述】:

我正在尝试将以下 2 个异步任务转换为 rxjava,但不知道如何去做。有任何想法吗? :

 new AsyncTask<Void, Void, Void>() {
                @Override
                protected Void doInBackground(Void... params) {

                    /* Shutdown video players */
                    Set<Map.Entry<String, PlayerBundle>> entries = videoPlayerPool.entrySet();
                    for (Map.Entry<String, PlayerBundle> entry : entries) {
                        PlayerBundle bundle = entry.getValue();
                        bundle.player.release();
                    }

                    /* Shutdown audio players */
                    entries = audioPlayerPool.entrySet();
                    for (Map.Entry<String, PlayerBundle> entry : entries) {
                        PlayerBundle bundle = entry.getValue();
                        bundle.player.release();
                    }

                    videoStreamer.stopStream();
                    videoStreamer.release();

                    audioStreamer.stopStream();
                    audioStreamer.release();

                    return null;
                }

                @Override
                protected void onPostExecute(Void aVoid) {
                    cb.event(new Spin.Event<Void>());
                }
            }.execute();

和:

new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                Set<Map.Entry<String, Participant>> entries = pool.entrySet();
                for (Map.Entry<String, Participant> entry : entries) {
                    Participant participant = entry.getValue();
                    participant.release();
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                cb.event(new Spin.Event<Void>());
            }
        }.execute();

我已经在我的 gradle 中包含了 rxjava,但不知道如何转换它

【问题讨论】:

标签: android android-asynctask rx-java rx-android rx-java2


【解决方案1】:
    Observable.defer(new Func0<Observable<Void>>() {
        @Override
        public Observable<Void> call() {
            /* Shutdown video players */
            Set<Map.Entry<String, PlayerBundle>> entries = videoPlayerPool.entrySet();
            for (Map.Entry<String, PlayerBundle> entry : entries) {
                PlayerBundle bundle = entry.getValue();
                bundle.player.release();
            }

            /* Shutdown audio players */
            entries = audioPlayerPool.entrySet();
            for (Map.Entry<String, PlayerBundle> entry : entries) {
                PlayerBundle bundle = entry.getValue();
                bundle.player.release();
            }

            videoStreamer.stopStream();
            videoStreamer.release();

            audioStreamer.stopStream();
            audioStreamer.release();

            return Observable.just(null);
        }
    })
    .doOnCompleted(new Action0() {
        @Override
        public void call() {
            cb.event(new Spin.Event<Void>());
        }
    })
    .subscribeOn(Schedulers.computation())
    .subscribe();

    Observable.defer(new Func0<Observable<Void>>() {
        @Override
        public Observable<Void> call() {
            Set<Map.Entry<String, Participant>> entries = pool.entrySet();
            for (Map.Entry<String, Participant> entry : entries) {
                Participant participant = entry.getValue();
                participant.release();
            }
            return Observable.just(null);
        }
    }).doOnCompleted(new Action0() {
        @Override
        public void call() {
            cb.event(new Spin.Event<Void>());
        }
    })
    .subscribeOn(Schedulers.computation())
    .subscribe();

【讨论】:

  • 以上 2 是 2 个不同的异步任务。是否可以分别进行。另外,func0 和 action0 是什么。难道不能像匿名班级那样做吗?或独立
  • 第二个任务以同样的方式转换。 Func0 和 Action0 是匿名类。这些是接口名称
  • 它给了我一个错误:无法解析符号 func0 和 Action0 。知道如何解决这个问题吗?
  • 确保你有这个导入:import rx.functions.Action0;导入 rx.functions.Func0;导入 rx.schedulers.Scheduler;
  • 是doOnCompleted还是doon完成?另外,我只是在实现: implementation 'io.reactivex.rxjava2:rxjava:2.1.1' implementation 'io.reactivex.rxjava2:rxandroid:2.0.1' ,我是否需要实现: implementation 'io.reactivex:rxjava: 1.0.14'也是?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多