【问题标题】:Convert AsyncTask to RxAndroid将 AsyncTask 转换为 RxAndroid
【发布时间】:2015-10-14 01:20:43
【问题描述】:

我有以下方法使用 otto 和 AsyncTask 发布对 UI 的响应。

private static void onGetLatestStoryCollectionSuccess(final StoryCollection storyCollection, final Bus bus) {
    new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... params) {
            bus.post(new LatestStoryCollectionResponse(storyCollection));
            return null;
        }
    }.execute();
}

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

【问题讨论】:

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


    【解决方案1】:

    不要使用 .create() 而是使用 .defer()

    Observable<File> observable = Observable.defer(new Func0<Observable<File>>() {
      @Override public Observable<File> call() {
    
        File file = downloadFile();
    
        return Observable.just(file);
      }
    });
    

    要了解更多详情,请参阅https://speakerdeck.com/dlew/common-rxjava-mistakes

    【讨论】:

    • 这对我来说似乎有点尴尬。它正在创建一个输出已下载文件的 Observable,该文件通过推迟创建恰好在后台执行。我相信 Observable.fromCallable 会更合适。
    【解决方案2】:

    这是一个使用 RxJava 进行文件下载任务的示例

    Observable<File> downloadFileObservable() {
        return Observable.create(new OnSubscribeFunc<File>() {
            @Override
            public Subscription onSubscribe(Observer<? super File> fileObserver) {
                try {
                    byte[] fileContent = downloadFile();
                    File file = writeToFile(fileContent);
                    fileObserver.onNext(file);
                    fileObserver.onCompleted();
                } catch (Exception e) {
                    fileObserver.onError(e);
                }
                return Subscriptions.empty();
            }
        });
    }
    

    用法:

    downloadFileObservable()
      .subscribeOn(Schedulers.newThread())
      .observeOn(AndroidSchedulers.mainThread())
      .subscribe(observer); // you can post your event to Otto here
    

    这将在新线程上下载文件并在主线程上通知您。

    OnSubscribeFunc 已弃用。代码更新为使用 OnSubscribe insted。欲了解更多信息,请参阅issue 802 on Github.

    Code from here.

    【讨论】:

    • 我无法使用此代码,因为:“错误:找不到符号类 OnSubscribeFunc”,在我的 gradle 中我编译了 'io.reactivex:rxandroid:1.0.1' 并编译了 'io.reactivex :rxjava:1.0.14'
    • @JachumbelechaoUntoMantekilla OnSubscribeFunc 已弃用。您可以改用OnSubscribe。我已经更新了我的答案并链接到了相关的 github 问题。
    • 取消订阅怎么样?如果当前的活动没有很快完成,这是必须的。对吗?
    • onSubscribe 被贬低
    【解决方案3】:

    在您的情况下,您可以使用fromCallable。更少的代码和自动onError 排放。

    Observable<File> observable = Observable.fromCallable(new Callable<File>() {
            @Override
            public File call() throws Exception {
                File file = downloadFile();
                return file;
            }
        });
    

    使用 lambda:

    Observable<File> observable = Observable.fromCallable(() -> downloadFile());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-21
      相关资源
      最近更新 更多