IConnectableObservable

IConnectableObservable

本文介绍了Rx中的IConnectableObservables的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释一个Observable和一个ConnectableObservable之间的区别吗?Rx扩展文档非常稀疏,我不明白在什么情况下ConnectableObservable有用.

Can someone explain the differences between an Observable and a ConnectableObservable? The Rx Extensions documentation is very sparse and I don't understand in what cases the ConnectableObservable is useful.

此类在Replay/Prune方法中使用.

This class is used in the Replay/Prune methods.

推荐答案

简短答案:

IConnectableObservable 表示可以与多个订户共享的待处理热观测.调用 IConnectableObservable.Connect()会使更改变为热的(可观察到冷源的订阅)

IConnectableObservable represents a pending hot observable that can be shared with multiple subscribers. Calling IConnectableObservable.Connect() causes the change to hot (subscribes to the cold source observable)

详细答案:

一个冷可观察的(如 Observable.Range )重播每个订户的序列.这与秒表类似,在秒表中,每个订户都有自己的秒表.订阅者通过订阅来启动秒表,一旦观察者停止观察,秒表就会停止(并重置).

A cold observable (like Observable.Range) replays the sequence for each subscriber. It's analagous to a stopwatch, where every subscriber is given their own stopwatch. The subscriber starts the stopwatch by subscribing, and the stopwatch stops (and resets) once the observer stops observing.

可热观测在所有订户之间共享序列.简而言之,就是只有一个秒表,并且所有订户都会获得相同的时间读数,而不管他们何时开始观看.

A hot observable shares the sequence between all subscribers. It's analagous to there being one stopwatch and all subscribers are given the same time readout, regardless of when they started watching.

IObservable.Publish 将冷的可观察对象转换为热的可观察对象,但返回 IConnectableObservable .这使订户可以在(单个)秒表启动之前对其进行订阅.调用 IConnectableObservable.Connect()将启动秒表.放置 Connect()返回值将停止秒表.

IObservable.Publish converts a cold observable into a hot observable, but returns an IConnectableObservable. This enables subscribers to subscribe to the (single) stopwatch before it starts. Calling IConnectableObservable.Connect() starts the stopwatch. Disposing the Connect() return value stops the stopwatch.

值得注意的是,某些可观察到的来源天生就很热.例如,无论是否订阅了鼠标事件,都可以触发鼠标事件.在这种情况下,所有可连接的可观察对象要做的就是共享一个事件订阅.

It's worth noting that some observable sources are hot by nature. For example, mouse events can fire regardless of whether we are subscribed to them. All a connectable observable would do in this scenario is a share a single event subscription.

这篇关于Rx中的IConnectableObservables的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 22:30