如何从一个ReplaySubject&LT的最新值; T>完成前 [英] How to get the latest value from a ReplaySubject<T> before completion

查看:93
本文介绍了如何从一个ReplaySubject&LT的最新值; T>完成前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要敛加入到ReplaySubject匹配特定标准的最近项的一种方式。样品code以下做什么,我需要做的,但它并不觉得自己是正确的做法:

 静态无效的主要(字串[] args)
{
    变种O =新ReplaySubject<字符串>();

    o.OnNext(蓝莓);
    o.OnNext(黑猩猩);
    o.OnNext(算盘);
    o.OnNext(香蕉);
    o.OnNext(苹果);
    o.OnNext(奶酪);

    变种最新= o.Where(ⅰ=> i.StartsWith(B))
        。.Latest()第一();

    Console.WriteLine(最新的);

    Console.WriteLine(preSS任意键退出);
    Console.ReadKey();
}
 

输出:

 香蕉
preSS任意键退出
 

起初,我尝试使用。凡()TakeLast(1)。不过,我现在知道从previous <一href="http://stackoverflow.com/questions/6760212/why-does-takelastt-method-not-work-on-a-replaysubjectt">question你必须调用的onComplete()之前主题TakeLast()返回任何东西。呼叫的onComplete()是不是一种选择,对我来说,因为我需要保持这种流开放。

任何人都可以请验证这是否是最有效的方法是什么,我试图完成?谢谢!

修改

请注意,我使用的是无扩展和IEnumerable code样品将无法正常工作。

更新

我倾向于以下code,因为我相信这是非阻塞的,除非有人可以告诉我是不同的:

 最新变种= o.Where(I =&GT; i.StartsWith(B))录像(1)。

使用(latest.Connect())
     latest.Subscribe(Console.WriteLine);
 

解决方案

您可以考虑使用 BehaviorSubject&LT;字符串&GT; 。其缺点是,你必须订阅的开始,但是这可能是你想要做什么呢。这应该为您提供的隔离需要。

 变种O =新ReplaySubject&LT;字符串&GT;();
变种BS =新BehaviorSubject&LT;字符串&GT;(默认值(字符串));
o.Where(ⅰ=&GT; i.StartsWith(B))订阅(BS)。

o.OnNext(蓝莓); Console.WriteLine(bs.First());
o.OnNext(黑猩猩); Console.WriteLine(bs.First());
o.OnNext(算盘); Console.WriteLine(bs.First());
o.OnNext(香蕉); Console.WriteLine(bs.First());
o.OnNext(苹果); Console.WriteLine(bs.First());
o.OnNext(奶酪); Console.WriteLine(bs.First());
 

输出:

 蓝莓
蓝莓
蓝莓
香蕉
香蕉
香蕉
 

I need a way of grabbing the most recent item added to a ReplaySubject that matches certain criteria. The sample code below does what I need it to do but it doesn't feel like the correct approach:

static void Main(string[] args)
{
    var o = new ReplaySubject<string>();

    o.OnNext("blueberry");
    o.OnNext("chimpanzee");
    o.OnNext("abacus");
    o.OnNext("banana");
    o.OnNext("apple");
    o.OnNext("cheese");

    var latest = o.Where(i => i.StartsWith("b"))
        .Latest().First();

    Console.WriteLine(latest);

    Console.WriteLine("Press any key to exit");
    Console.ReadKey();
}

Output:

banana
Press any key to exit

Initially, I tried using .Where().TakeLast(1); however, I now know from a previous question that you must call OnComplete() on the subject before TakeLast() will return anything. Calling OnComplete() is not an option for me because I need to keep this stream open.

Can anyone please validate whether this is the most effective approach to what I'm trying to accomplish? Thanks!

EDIT

Please note that I'm using Reactive Extensions and IEnumerable code samples will not work.

UPDATE

I'm leaning towards the following code because I believe it is non-blocking unless anyone can tell me differently:

var latest = o.Where(i => i.StartsWith("b")).Replay(1);

using (latest.Connect())
     latest.Subscribe(Console.WriteLine);

解决方案

You may consider using BehaviorSubject<string>. The drawback is that you have to subscribe at the beginning but that is probably what you want to do anyway. This should provide you with isolation you need.

var o = new ReplaySubject<string>();
var bs = new BehaviorSubject<string>(default(string));
o.Where(i => i.StartsWith("b")).Subscribe(bs);

o.OnNext("blueberry"); Console.WriteLine(bs.First());
o.OnNext("chimpanzee"); Console.WriteLine(bs.First());
o.OnNext("abacus"); Console.WriteLine(bs.First());
o.OnNext("banana"); Console.WriteLine(bs.First());
o.OnNext("apple"); Console.WriteLine(bs.First());
o.OnNext("cheese"); Console.WriteLine(bs.First());

Output:

blueberry
blueberry
blueberry
banana
banana
banana

这篇关于如何从一个ReplaySubject&LT的最新值; T&GT;完成前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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