The Replay
operator returns an IConnectableObservable<T>
, which is an IObservable<T>
with an extra Connect
method. This observable can be subscribed by any number of observers. It propagates to all observers all past and future notifications coming from the underlying observable, starting from the time it was connected and ending to the time it was disconnected. Here is an example:
var connectable = Observable
.Range(1, 100)
.SelectMany(x => GetItemAsync(x))
.Replay();
var subscription1 = connectable.Subscribe(x => Console.WriteLine(x))
var subscription2 = connectable.Subscribe(x => Console.WriteLine(x))
var connection = connectable.Connect(); // Subscribe to the 'SelectMany' observable
//...
connection.Dispose() // Unsubscribe from the 'SelectMany' observable
This example demonstrates how to connect manually to the underlying observable, which is important when using other multicast operators like the Publish
. But it's less important with the Replay
operator because of its replay functionality: it doesn't matter whether it will be subscribed before or after it connects to the underlying observable. So you may choose to avoid connecting manually, and use one of the two available auto-connect operators:
RefCount
: connects to the underlying observable when it is subscribed for the first time, and disconnects when its last subscriber unsubscribes.
AutoConnect(0)
: connects to the underlying observable immediately, and stays connected forever.
Example:
var observable = Observable
.Range(1, 100)
.SelectMany(x => GetItemAsync(x))
.Replay()
.AutoConnect(0);
// call observable.Subscribe any time and as many times you want
Both RefCount
and AutoConnect
also disconnect automatically when the underlying observable completes successfully or with an error.
Connecting and disconnecting more than once is not a supported scenario, and may produce unexpected results. If you want to disconnect and reconnect, you'd better use a different Replay
connectable each time.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…