Replay和RefCount的有趣行为 [英] Interesting behaviour with Replay and RefCount
本文介绍了Replay和RefCount的有趣行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请参阅下面的代码。在我当前的版本中,我看到result1的一个值输出,result2的两个值和result3的三个值。由于重播将使用单个ReplaySubject和RefCount()将连接/断开每个取消订阅,我想这个
是预期的行为?但这不是一个明显的问题。
See the code below. In my current version, I see one value output from result1, two values from result2 and three values from result3. Since replay will use a single ReplaySubject and RefCount() will connect/disconnect on each unsubscription, I guess this is intended behaviour? It's not an obvious one though.
var timerStream = Observable.Timer(TimeSpan.Zero, TimeSpan.FromMinutes(10));
var stream =
timerStream.Replay().RefCount();
"1 Subscribing".Dump();
var result1 = stream.Subscribe(_ => "1 Got Value".Dump());
"1 Disposing".Dump();
result1.Dispose();
"2 Subscribing".Dump();
var result2 = stream.Subscribe(_ => "2 Got Value".Dump());
"2 Disposing".Dump();
result2.Dispose();
"3 Subscribing".Dump();
var result3 = stream.Subscribe(_ => "3 Got Value".Dump());
"3 Disposing".Dump();
result3.Dispose();
这是我看到的输出:
1 Subscribing
1 Disposing
2 Subscribing
2 Got Value
2 Got Value
2 Disposing
3 Subscribing
3 Got Value
3 Got Value
3 Got Value
3 Disposing
推荐答案
嗨雷,
Rxx 提供了设计的多播运营商的
重载避免此类行为。 还有一个相关的
实验室。
Rxx provides an overload of the Multicast operator that was designed to avoid such behavior. There's also a related lab.
- 戴夫
这篇关于Replay和RefCount的有趣行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文