何时在Android中使用RxJava以及何时从Android Architectural Components使用LiveData? [英] When to use RxJava in Android and when to use LiveData from Android Architectural Components?

查看:91
本文介绍了何时在Android中使用RxJava以及何时从Android Architectural Components使用LiveData?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有理由在Android中使用RxJava并从Android Architectural Components中使用LiveData.如果以代码形式说明示例之间的用例和差异以及两者之间的差异,这将非常有帮助两者.

I am not getting the reason to use RxJava in Android and LiveData from Android Architectural Components.It would be really helpful if the usecases and differences between the both are explained along with sample example in the form of code which explains the differences between the both.

推荐答案

Android LiveData是原始观察者模式的变体,其中添加了活动/非活动过渡.因此,它的范围非常严格.

Android LiveData is a variant of the original observer pattern, with the addition of active/inactive transitions. As such, it is very restrictive in its scope.

使用 Android LiveData 中描述的示例,创建一个类监视位置数据,并根据应用程序状态进行注册和注销.

Using the example described in Android LiveData, a class is created to monitor location data, and register and unregister based on application state.

RxJava提供了更通用的运算符.假设此可观察对象将提供位置数据:

RxJava provides operators that are much more generalized. Let's assume that this observable will provide location data:

Observable<LocationData> locationObservable;

可以使用Observable.create()映射回叫操作来构建可观察对象的实现.当观察对象被订阅时,回调被注册,而当它被取消订阅时,回调被取消注册.该实现看起来与示例中提供的代码非常相似.

The implementation of the observable can be built up using Observable.create() to map the call back operations. When the observable is subscribed, the call back is registered, and when it is unsubscribed, the call back is unregistered. The implementation looks very similar to the code provided in the example.

我们还假设您有一个可观察的对象,当应用程序处于活动状态时会发出true:

Let's also assume that you have an observable that emits true when the application is active:

Observable<Boolean> isActive;

然后您可以通过以下方式提供LiveData的所有功能

Then you can provide all the functionality of LiveData by the following

Observable<LocationData> liveLocation =
  isActive
    .switchMap( active -> active ? locationObservable : Observable.never() );

switchMap()运算符将以流的形式提供当前位置,如果应用程序未处于活动状态,则什么都不提供.一旦可以观察到liveLocation,就可以使用RxJava运算符对其进行很多操作.我最喜欢的示例是:

The switchMap() operator will either provide the current location as a stream, or nothing if the application is not active. Once you have the liveLocation observable, there a lot of things you can do with it using RxJava operators. My favorite example is:

liveLocation.distinctUntilChanged()
  .filter( location -> isLocationInAreaOfInterest( location ) )
  .subscribe( location -> doSomethingWithNewLocation( location ) );

仅当位置更改且位置有趣时,该操作才会执行.您可以创建类似的操作来 结合时间运算符来确定速度.更重要的是,您可以使用RxJava运算符提供对操作是在主线程,后台线程还是多个线程中进行的详细控制.

That will only perform the action when the location changed, and the location is interesting. You can create similar operations that combine time operators to determine speed. More importantly, you can provide detailed control of whether operations happen in the main thread, or a background thread, or a multiple threads, using RxJava operators.

RxJava的要点是,它使用库中提供的操作甚至您提供的自定义操作将控制和计时结合到一个Universe中.

The point of RxJava is that it combines control and timing into a single universe, using operations provided from the library, or even custom operations that you provide.

LiveData仅处理该Universe的一小部分,相当于构建liveLocation.

LiveData addresses only one small part of that universe, the equivalent of building the liveLocation.

这篇关于何时在Android中使用RxJava以及何时从Android Architectural Components使用LiveData?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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