通过的IObservable收到最新值:无 [英] Reactive: Latest value to be received by IObservable

查看:157
本文介绍了通过的IObservable收到最新值:无的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道下面是一个阻塞调用,将在观察到的序列返回的第一个值:

I know that the following is a blocking call and will return the first value in the observable sequence:

var result = myObservable.First();



根据我使用什么类型的题目这有不同的含义:

Depending on what type of subject I use this has different implications:


  • 主题 - 第一()将阻塞,直到OnNext()一次调用,这意味着最终的这将是最新值

  • BehaviorSubject - 第一()将阻塞,直到至少有一个值已通过OnNext(推),也因为BehaviorSubject跟踪的最后一个值这将是最新值

  • ReplaySubject - 第一()将阻塞,直到至少一个值已通过OnNext()推压,但在情况下,许多项目已通过OnNext被推这将是第一个在其缓冲器的这是不是最后一个

  • Subject - First() will block until the OnNext() is next called which means eventually this will be the latest value
  • BehaviorSubject - First() will block until at least one value has been pushed through OnNext() and because BehaviorSubject tracks the last value this will be the latest value
  • ReplaySubject - First() will block until at least one value has been pushed through OnNext(), but in the case that many items have been pushed through OnNext it will be the first on in its buffer which is NOT the last one

现在我想找到获得的最后一个值的一致方法不管是哪个底层是用来观察的。

Now I am trying to find a consistent way of getting the last value regardless of which underlying Observable is used.

任何想法?

推荐答案

你的其他接收的问题,我想你想要这样的:

Based on your other Rx question, I think you want this:

var rootSubject = new ReplaySubject<Types>();
var firstSubject = rootSubject.Where(x => x == Types.First);
var secondSubject = rootSubject.Where(x => x == Types.Second);
var thirdSubject = rootSubject.Where(x => x == Types.Third);
var forthSubject = rootSubject.Where(x => x == Types.Fourth);

var mergedSubject = Observable
              .Merge(firstSubject, secondSubject, thirdSubject, forthSubject)
        .Timeout(TimeSpan.FromSeconds(2), Observable.Return(Types.Error))    
        .Replay();

mergedSubject.Connect();

rootSubject.OnNext(Types.First);
rootSubject.OnNext(Types.Second);

var result = mergedSubject.First();

rootSubject.OnNext(Types.Third);
rootSubject.OnNext(Types.Fourth);

Console.WriteLine(String.Format("result - {0}", result));

现在它不会不管什么样的主题时,他们都回归结果 - 第一 。

Now it doesn't matter what kind of Subject is used, they all return "result - First".

如果你调用mergedSubject.First(),你会使用重播(1)之前想要最新的值:

If you want the latest value before calling mergedSubject.First(), you'd use Replay(1):

var mergedSubject = Observable
                .Merge(firstSubject, secondSubject, thirdSubject, forthSubject)
                .Timeout(TimeSpan.FromSeconds(2), Observable.Return(Types.Error))    
                .Replay(1);

在这种情况下,所有的主题类型会回来。结果 - 第二届

In which case, all Subject types will return "result - Second".

这篇关于通过的IObservable收到最新值:无的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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