RxJava 中 Observable、Completable 和 Single 的区别是什么 [英] What is the difference between Observable, Completable and Single in RxJava

查看:99
本文介绍了RxJava 中 Observable、Completable 和 Single 的区别是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能用清晰的例子解释 RxJava 中 Observable、Completable 和 Single 之间的区别?

在哪种情况下我们使用一种而不是另一种?

解决方案

Observable 是通用的 ReactiveX 构建块,事件源随时间发出值.(因此存在于 ReactiveX 扩展到的每种语言中)
简而言之,可观察到的事件是:
onNext* (onCompleted | onError)?/(* 零个或多个 ? - 零或 1)

SingleCompletable 是在 RxJava 中专门引入的新类型,表示Observable 的简化类型代码>,具有更简洁的API.

Single 表示发出单个值或错误的 Observable.

Completable 代表 Observable 不发出任何值,只发出终端事件,onErroronCompleted

您可以将这些差异视为返回方法的差异:

  • 对象集合 - 可观察

  • 单个对象 - 单个

  • 和不返回值的方法(void 方法) - Completable.

Single 当您有面向任务的 Observable 并且您期望单个值时可能是合适的,例如执行一次并返回值(或错误)的网络请求,以一次方式操作网络调用,这意味着您不希望它随着时间的推移返回额外的值.另一个例子是DB取数据操作.

Completable 适用于当你有一个 Observable 并且你不关心操作产生的值,或者没有任何值.例如更新缓存,操作可以成功/失败,但没有价值.
另一个例子是一些长时间运行的 init 操作,它不返回任何内容.可以是 UPDATE/PUT 网络调用,结果只有成功指示.

在任何情况下,Completable 和 Single 都没有添加新功能,而是引入了更健壮和简洁的 API,这将更多地说明 API 公开的 Observable 背后的操作.

RxJava2 也许:

RxJava2 增加了一个新的类型叫做 MaybeMaybeCompletable 和 Single 的组合.

在与上述相同的语言中,Maybe 可以被认为是一个返回的方法Optional 某种类型,Optional 是 Object 的包装器,它明确地告诉我们是否有一些值 - Object 与否(而不是 null).
使用 Maybe,我们可以有一些与 Single 完全一样的值,或者什么都不返回——就像 Completable 一样.此外,与两者一样,我们也有错误.
Maybe 当我们想要标记一个 Observable 可能没有值并且只会完成时是有价值的.
将从缓存中获取一个示例,我们不一定在缓存中有一个值,因此在这种情况下,我们将完成,o.w.我们将使用缓存中的值获取 onNext.
这也值得用 RxJava2 处理流中的非空值.

RxJava2 Flowable:

首先,让我们定义背压.背压是一种处理数据生成速度快于处理速度的情况的方法.Flowable 具有背压支持,允许下游请求项目.您可以在此处阅读有关差异的更多信息.

Can anyone please explain the difference between Observable, Completable and Single in RxJava with clear examples?

In which scenario we use one over the others?

解决方案

Observable is the generic ReactiveX building block, of event source that emits values over time. (and thus exists in every language ReactiveX extended to)
in short Observable events are:
onNext* (onCompleted | onError)? /(* zero or more ? - zero or 1)

Single and Completable are new types introduced exclusively at RxJava that represent reduced types of Observable, that have more concise API.

Single represent Observable that emit single value or error.

Completable represent Observable that emits no value, but only terminal events, either onError or onCompleted

You can think of the differences like the differences of a method that returns:

  • Collection of Objects - Observable

  • Single object - Single

  • and method that returns no values (void method) - Completable.

Single can be appropriate when you have task oriented Observable and you expect single value, like Network request which is performed once and return value (or error), network call is operated in one time fashion, meaning you don't expect it to return additional values over time. Another example is DB fetch data operation.

Completableis appropriate when you have an Observable and you don't care about the value resulted from the operation, or there isn't any. Examples are updating a cache for instance, the operation can either succeed/fail, but there is no value.
Another example is some long running init operation that doesn't return anything. It can be UPDATE/PUT network call that resulted with success indication only.

In any case, Completable and Single are not adding new capabilities but they are introducing more robust and concise APIs, that tells more about the operations behind the Observable that the API exposed.

Edit:

RxJava2 Maybe:

RxJava2 added a new type called Maybe, Maybe is the combination of Completable and Single.

In the same language like above, Maybe can be thought of as a method that returns Optional of some type, Optional is a wrapper around Object that explicitly tells whether we have some value in it - Object or not (instead of null).
With Maybe we can either have some value exactly like Single or have return nothing - just like Completable. Additionally, like both, we have the error.
Maybe is valuable when we want to mark that an Observable might not have a value and will just complete.
An example would be fetched from the cache, we'll not necessarily have a value in the cache, so in this case, we will complete, o.w. we will get onNext with the value from the cache.
This is also worthy to handle non-null values in a stream with RxJava2.

RxJava2 Flowable:

First, let's define backpressure. Backpressure is a means of handling the situation where data is generated faster than it can be processed. Flowable has backpressure support allowing downstream to request items. You can read more about the differences here.

这篇关于RxJava 中 Observable、Completable 和 Single 的区别是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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