RxJava 2.0中的Observable和Flowable有什么区别? [英] What is the difference between Observable and Flowable in RxJava 2.0?

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

问题描述

Observable和Flowable接口似乎完全相同。为什么Flowable是在RxJava 2.0中引入的?我何时应该使用Flowable over Observable?

Observable and Flowable interfaces seem to be identical. Why Flowable was introduced in RxJava 2.0? When should I prefer to use Flowable over Observable?

推荐答案

文档


关于在RxJava 0.x中引入背压的一个小遗憾是
而不是单独的基础反应类,Observable
本身是改装的。背压的主要问题是许多
热源(如UI事件)无法合理地反压,
导致意外 MissingBackpressureException (即,初学者不要
期待他们。)

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. The main issue with backpressure is that many hot sources, such as UI events, can't be reasonably backpressured and cause unexpected MissingBackpressureException (i.e., beginners don't expect them).

我们试图通过让
io来补救2.x中的这种情况.reactivex.Observable 非背压和新的
io.reactivex.Flowable 是启用背压的基础反应类。

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.

当一段时间内物品相对较少(< 1000)时使用 Observable /或者没有风险生产者过度消费消费者,从而导致OOM。

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.

使用可流动的当你有相对大量的物品,你需要仔细控制生产者的行为,以避免资源耗尽和/或拥堵。

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.

Backpressure
当你有一个ob servable如此快速地发出物品,消费者无法跟上流量,导致已发射但未消耗的物品存在。

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.

可观察物品发出的未消耗物品的数量但是没有被订阅者消费,被管理和控制的是背压策略所处理的。

How unconsumed items, which are emitted by observables but not consumed by subscribers, are managed and controlled is what backpressure strategy deals with.

参考链接

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

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