在Android的RxJava异步任务 [英] RxJava Async task in Android

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

问题描述

我想实现在Android中使用RxJava异步任务。
我尝试以下code和它没有工作。它执行在UI线程上。我使用RxAndroid 0.24.0以下版本。

  {尝试
            Observable.just(someMethodWhichThrowsException())
                    .subscribeOn(Schedulers.newThread())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(秒 - > onMergeComplete());
        }赶上(IOException异常五){
            e.printStackTrace();
        }

但是,下面的异步工作对我来说。

 可观察到的可观测= Observable.create(新Observable.OnSubscribe<串GT;(){
            @覆盖
            公共无效电话(订户LT ;?超级字符串>用户){
                尝试{
                    someMethodWhichThrowsException();
                }赶上(IOException异常五){
                    e.printStackTrace();
                }                subscriber.onCompleted();
            }
        });
        observable.subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe();

我想了解以下内容:
 1.它们之间有什么区别?
 2.什么是最好的做法,同时创造异步任务?

先谢谢了。


解决方案

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

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

这是等效于以下:

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

正如你可以看到这使得同步方法调用第一的然后的把它传递给 Observable.just 成为观察的。

  Observable.create(新Observable.OnSubscribe<串GT;(){
        @覆盖
        公共无效电话(订户LT ;?超级字符串>用户){
            ...
        }
    })
    .subscribeOn(Schedulers.newThread())
    .observeOn(AndroidSchedulers.mainThread())
    。订阅();

这个方法,但是,将运行在呼叫块的上订阅的的code。你告诉它你要订阅一个新的线程( subscribeOn(Schedulers.newThread())),所以订阅发生在一个新的线程,而$ C它获取的认购(即呼叫块)上运行$ C被该线程运行了。这是类似的行为调用 Observable.defer

<醇开始=2>
  • 什么是最好的做法,同时创造异步任务?

  • 嗯,这是给你和你的期望的行为。有时你想异步code立即开始运行(在这种情况下,你可能想使用运营商的目的之一是缓存)。我肯定会考虑使用异步utils的库这一点。

    其他时候,你会希望它只能在认购运行(在例子中的行为在这里) - 例如,如果有副作用,或者如果你在它的运行不在乎,只是想使用内置的插件得到的东西掉在UI线程。 丹卢提到 Observable.defer 是一个转换接收过程中采取旧的code和得到它关闭UI线程,非常方便。

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

    I am trying to understand the following: 1. What is the difference between them? 2. What is the best practice while creating async tasks?

    Thanks in advance.

    解决方案

    1. What is the difference between them?

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

    This is equivalent to the following:

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

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

    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. What is the best practice while creating async tasks?

    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.

    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天全站免登陆