【问题标题】:How do I keep count of Hot observables?如何计算 Hot observables?
【发布时间】:2013-09-12 11:09:41
【问题描述】:

我有以下 Observable。

IObservable<MyDto> observable;

现在为了计算 Observable 中的项目数,我无法使用以下代码,因为我的 observable 是 Hot 且长时间运行,并且永远不会调用对“计数”的订阅。

var count = observable.Count()

我希望每次物品到达时都得到计数,这是我正在尝试做的事情

observable.Subscribe(o => Console.WriteLine(" Object received "));
observable.Count().Subscribe(c => Console.WriteLine("Current count is " + c.ToString() + " but this is not final count, more are coming"));

我怎样才能做到这一点?

【问题讨论】:

    标签: c# system.reactive reactive-programming observable


    【解决方案1】:

    使用Scan:

    observable
        .Scan(0, (count, _) => count + 1)
        .Subscribe(count => Console.WriteLine("Current count is " + count));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-18
      • 1970-01-01
      • 2016-07-02
      • 2014-08-28
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      相关资源
      最近更新 更多