简体   繁体   English

RxJava 2.0中的Observable和Flowable有什么区别?

[英]What is the difference between Observable and Flowable in RxJava 2.0?

Observable and Flowable interfaces seem to be identical. 可观察和可流动的界面似乎是相同的。 Why Flowable was introduced in RxJava 2.0? 为什么Flowable是在RxJava 2.0中引入的? When should I prefer to use Flowable over Observable? 我什么时候应该使用Flowable over Observable?

As stated in the documentation : 文档所述

A small regret about introducing backpressure in RxJava 0.x is that instead of having a separate base reactive class, the Observable itself was retrofitted. 关于在RxJava 0.x中引入背压的一个小遗憾是,Observable本身是改装的,而不是单独的基础反应类。 The main issue with backpressure is that many hot sources, such as UI events, can't be reasonably backpressured and cause unexpected MissingBackpressureException (ie, beginners don't expect them). 背压的主要问题是许多热源(例如UI事件)无法合理地反压并导致意外的MissingBackpressureException (即初学者不期望它们)。

We try to remedy this situation in 2.x by having io.reactivex.Observable non-backpressured and the new io.reactivex.Flowable be the backpressure-enabled base reactive class. 我们尝试通过使io.reactivex.Observable非背压和新的io.reactivex.Flowable为背压启用的基础反应类来纠正2.x中的这种情况。

Use Observable when you have relatively few items over time (<1000) and/or there's no risk of producer overflooding consumers and thus causing OOM. 当一段时间内项目相对较少(<1000)时使用Observable和/或没有生产者过度消费消费者的风险 ,从而导致OOM。

Use Flowable when you have relatively large amount of items and you need to carefully control how Producer behaves in order to to avoid resource exhaustion and/or congestion. 当您拥有相对大量的项目时,请使用Flowable ,并且您需要仔细控制Producer行为方式,以避免资源耗尽和/或拥塞。


Backpressure When you have an observable which emits items so fast that consumer can't keep up with the flow leading to the existence of emitted but unconsumed items. Backpressure当你有一个可观察的物品如此快速地发射物品时,消费者无法跟上流量导致已发射但未消耗物品的存在。

How unconsumed items, which are emitted by observables but not consumed by subscribers, are managed and controlled is what backpressure strategy deals with. 背压策略处理的是由观察者发出但未被订户消费的未消耗物品如何被管理和控制。

Ref link 参考链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM