何时在 Android 中使用 RxJava,何时使用 Android 架构组件中的 LiveData? [英] When to use RxJava in Android and when to use LiveData from Android Architectural Components?

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

问题描述

我没有得到在 Android 中使用 RxJava 和在 Android 架构组件中使用 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 提供了更通用的操作符.让我们假设这个 observable 将提供位置数据:

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

Observable<LocationData> locationObservable;

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

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 的 observable:

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 observable,你可以使用 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 的重点在于,它使用库提供的操作,甚至您提供的自定义操作,将控制和计时组合到一个单一的宇宙中.

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 仅解决该领域的一小部分,相当于构建 liveLocation.

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

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

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