Reactive Extensions 是否评估了太多次? [英] Is Reactive Extensions evaluating too many times?

查看:28
本文介绍了Reactive Extensions 是否评估了太多次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Reactive Extensions 应该评估它的各种操作符多少次?

How many times is Reactive Extensions supposed to evaluate its various operators?

我有以下测试代码:

var seconds = Observable
    .Interval(TimeSpan.FromSeconds(5))
    .Do(_ => Console.WriteLine("{0} Generated Data", DateTime.Now.ToLongTimeString()));

var split = seconds
    .Do(_ => Console.WriteLine("{0}  Split/Branch received Data", DateTime.Now.ToLongTimeString()));

var merged = seconds
    .Merge(split)
    .Do(_ => Console.WriteLine("{0}   Received Merged data", DateTime.Now.ToLongTimeString()));

var pipeline = merged.Subscribe();

我希望它每五秒写入一次生成的数据".然后,它将该数据传递给写入拆分/分支接收数据"的拆分"流和写入接收的合并数据"的合并"流.最后,因为 'merged' 流也从 'split' 流接收,它第二次接收数据并第二次写入Received Merged data".(它写入其中一些的顺序不是特别相关)

I expect this to write "Generated Data" once every five seconds. It then hands off that data to both the 'split' stream which writes "Split/Branch received Data", and to the 'merged' stream which writes "Received Merged data". Last, because the 'merged' stream is also receiving from the 'split' stream, it receives the data second time and writes "Received Merged data" a second time. (The order it writes some of them in is not particularly relevant)

但我得到的输出是这样的:

But the output I am getting is this:

8:29:56 AM Generated Data
8:29:56 AM Generated Data
8:29:56 AM  Split/Branch received Data
8:29:56 AM   Received Merged data
8:29:56 AM   Received Merged data
8:30:01 AM Generated Data
8:30:01 AM Generated Data
8:30:01 AM  Split/Branch received Data
8:30:01 AM   Received Merged data
8:30:01 AM   Received Merged data

它正在写入生成数据"两次.据我了解,订阅秒"IObservable 的下游观察者的数量不应影响生成的数据"写入的次数(应该是一次),但确实如此.为什么?

It is writing "Generaged Data" twice. To my understanding, The number of downstream observers that are subscribed to the "seconds" IObservable should not affect the number of times that "Generated Data" writes (which should be ONCE), but it does. Why?

注意 我在 .Net Framework 3.5 环境中使用稳定版 v1.0 SP1 的反应式扩展.

NOTE I am using the stable release v1.0 SP1 of reactive extensions in a .Net Framework 3.5 environment.

推荐答案

据推测,他们选择这种方法是为了允许每个订阅者在与初始订阅相同的时间间隔内获取其值.考虑您的备用间隔将如何工作:

Presumably, they choose that approach to allow each subscriber to get its values at the same interval from their initial subscription. Consider how your alternate Interval would work:

0s - First subscriber subscribes
5s - Value: 0
8s - Second subscriber subscribes
10s - Value: 1
15s - Value: 2
17s - Unsubscribe both

你最终得到的是这样的:

What you end up with is something like this:

First  -----0----1----2-|
Second         --1----2-|

在这种情况下,两个观察者有明显不同的结果,这取决于是否已经附加了任何其他观察者.在实施过程中,Interval 为每个订阅者提供相同的体验,无论顺序或过去的订阅者如何.

In this case, the two observers have noticeably different results depending on whether or not there is any other observer already attached. As it is implemented, Interval gives the same experience to each subscriber regardless of order or past subscribers.

综上所述,您可以通过在创建 seconds<时添加 .Publish().RefCount() 来转换"Interval 为您描述的行为/code> 可观察的.

All that said, you can "convert" Interval to the behavior you describe by adding .Publish().RefCount() when creating the seconds observable.

这篇关于Reactive Extensions 是否评估了太多次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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