Why are Flowables not Observables

痞子三分冷 提交于 2019-12-23 12:28:55

问题


Why are Flowables not Observables; Observable interface is pretty much a subset of Flowable, their implementations are pretty much the same.

Why don't they implement a common interface so we can directly cast Flowable as Observable?


回答1:


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).

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.

https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0#observable-and-flowable




回答2:


Why are Flowables not Observables

Conceptional separation. I would have gone with Flowable only because that can do all the other types. The community, however, were strongly into reflecting the major behavioral property in separate types: Single, Maybe, Completable...

Observable interface is pretty much a subset of Flowable

This is a common viewpoint from people who likely don't program on a daily basis. Unfortunately for them, the type system and language (Java) doesn't let such high-level abstractions get specialized easily or at all.

Plus, implementing backpressure requires specific algorithms and building blocks and can get quite complicated. I encourage you to check out the difference between one of the more difficult operators: Flowable.flatMapIterable vs Observable.flatMapIterable.

Why don't they implement a common interface so we can directly cast Flowable as Observable

Flowable implements the Reactive-Streams interfaces and thus we can't just declare Subscriber extends Observer and Subscription extends Disposable.

Furthermore, at one time in early v2, Observable implemented Publisher and caused massive amounts of ambiguity and manual casting would have been a major pain point for library users.



来源:https://stackoverflow.com/questions/43876069/why-are-flowables-not-observables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!