Android 中的 RxJava 异步任务 [英] RxJava async task in Android

查看:24
本文介绍了Android 中的 RxJava 异步任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Android 中使用 RxJava 实现异步任务.我尝试了以下代码,但没有用.它在 UI 线程上执行.我使用的是以下版本的 RxAndroid 0.24.0.

I am trying to implement an asynchronous task using RxJava in Android. I tried the following code and it didn't work. It executes on the UI thread. I am using the following version of RxAndroid 0.24.0.

try {
    Observable.just(someMethodWhichThrowsException())
            .subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(s -> onMergeComplete());
}
catch (IOException e) {
    e.printStackTrace();
}

但是,以下对我来说是异步的.

However, the following works asynchronously for me.

Observable observable = Observable.create(new Observable.OnSubscribe<String>() {
        @Override
        public void call(Subscriber<? super String> subscriber) {
            try {
                someMethodWhichThrowsException();
            } catch (IOException e) {
                e.printStackTrace();
            }

            subscriber.onCompleted();
        }
    });
    observable.subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe();

我试图理解以下内容:

  1. 它们之间有什么区别?
  2. 创建异步任务的最佳做法是什么?

推荐答案

  1. 它们之间有什么区别?

Observable.just(someMethodWhichThrowsException())
    .subscribeOn(Schedulers.newThread())

这相当于:

Object someResult = someMethodWhichThrowsException();
Observable.just(someResult)
    .subscribeOn(Schedulers.newThread())

如您所见,这首先调用同步方法,然后将其传递给 Observable.just 成为一个 Observable.

As you can see this makes the synchronous method call first, then passes it to Observable.just to become an Observable.

Observable.create(new Observable.OnSubscribe<String>() {
        @Override
        public void call(Subscriber<? super String> subscriber) {
            ...
        }
    })
    .subscribeOn(Schedulers.newThread())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe();

然而,此方法将运行call块中的代码订阅.你已经告诉它你想订阅一个新线程 (subscribeOn(Schedulers.newThread())),所以订阅发生在一个新线程上,并且在订阅时运行的代码(call 块)也会在该线程上运行.这与调用 Observable.defer 的行为类似.>

This method, however, will run the code in the call block on subscription. You've told it you want to subscribe on a new thread (subscribeOn(Schedulers.newThread())), so the subscription happens on a new thread, and the code which gets run on subscription (the call block) gets run on that thread too. This is similar behaviour to calling Observable.defer.

  1. 创建异步任务的最佳做法是什么?

好吧,这取决于您和您想要的行为.有时您希望异步代码立即开始运行(在这种情况下,您可能希望使用其中一个运算符来缓存它).我肯定会考虑为此使用 Async Utils 库.

Well, that's up to you and your desired behaviour. Sometimes you want the async code to begin running immediately (in which case you may want to cache it using one of the operators for that purpose). I'd definitely consider using the Async Utils library for this.

其他时候,您会希望它仅在订阅时运行(这是此处示例中的行为) - 例如,如果有副作用,或者如果您不关心它何时运行而只想使用内置函数从 UI 线程中获取一些东西.Dan Lew 提到 Observable.defer 在转换为 Rx 期间,非常方便地将旧代码从 UI 线程中取出.

Other times, you'll want it to run only on subscription (which is the behaviour in the examples here) - for example if there are side-effects, or if you don't care when it's run and just want to use the built-ins to get something off the UI thread. Dan Lew mentions that Observable.defer is very handy for taking old code and getting it off the UI thread, during a conversion to Rx.

这篇关于Android 中的 RxJava 异步任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆