[英]Getting the latest item in an observable sequence using RX in C#
以以下为例:
var ob = Observable.Interval(TimeSpan.FromSeconds(1)).StartWith(500).Replay(1).RefCount();
我在这里试图实现的是在任何给定时间“同步”获取序列中最新项目的值。 这意味着像FirstAsync
这样的扩展无法弥补我的FirstAsync
。
StartWith
和Replay
位确保总是有一个值,并且在我的实际代码中需要RefCount
位来检测我何时可以执行一些处理操作。
所以为了模拟这个“任何给定时间”的部分,让我们尝试在 5 秒后获取最新值:
Observable.Timer(TimeSpan.FromSeconds(5)).Subscribe(x =>
{
// Try to get latest value from "ob" here.
});
所以有 5 秒的延迟,我需要从序列中获取值5
,这些是我迄今为止尝试过但没有成功的方法:
ob.First()
- 返回 500ob.Latest().Take(1)
- 同上ob.MostRecent(-1).First()
- 同上ob.MostRecent(-1)
- 给我一个IEnumerable<long>
充满“500”ob.Last()
- 永远不会返回,因为它正在等待它永远不会完成的序列ob.Latest().Last()
- 同上ob.ToTask().Result
- 同上ob.ToEnumerable()
- 同上ob.MostRecent().Last()
同上似乎没有太多资源可以让人们真正做到这一点。 我能找到的最接近的是:“ Rx:用于从 Observable 流中获取第一个和最新值的操作符”,但它毕竟不是同步调用(仍然使用订阅),所以它对我不起作用。
有没有人知道这是否真的可行?
指出为什么你的代码可能没有像你期望的那样工作
var ob = Observable.Interval(TimeSpan.FromSeconds(1)).StartWith(500).Replay(1).RefCount();
//Note at this point `ob` has never been subscribed to,
// so the Reference-count is 0 i.e. has not be connected.
Observable.Timer(TimeSpan.FromSeconds(5)).Subscribe(x =>
{
// Try to get latest value from "ob" here.
//Here we make our first subscription to the `ob` sequence.
// This will connect the sequence (invoke subscribe)
// which will
// 1) invoke StartWith
// 2) invoke onNext(500)
// 3) invoke First()
// 4) First() will then unsubscribe() as it has the single value it needs
// 5) The refCount will now return to 0
// 6) The sequence will be unsubscribed to.
ob.First().Dump();
//Any future calls like `ob.First()` will thus always get the value 500.
});
可能你想要的是
var ob = Observable.Interval(TimeSpan.FromSeconds(1))
.Publish(500);
var connection = ob.Connect();
//Note at this point `ob` has never been subscribed to, so the ReferenceCount is 0 i.e. has not be connected.
var subscription = Observable.Timer(TimeSpan.FromSeconds(5)).Subscribe(x =>
{
// Try to get latest value from "ob" here.
ob.First().Dump();
});
//Sometime later
subscription.Dispose();
connection.Dispose()
但是,您真的不想将同步调用与 Rx 混合在一起。 您通常也不想在订阅中订阅(因为.First()
是订阅)。 您可能想要做的是获取最新值,并将其存放在某处。 使用.First()
只是一个滑坡。 你可能会更好地写一些类似的东西
var subscription = Observable.Timer(TimeSpan.FromSeconds(5))
.SelectMany(_=>ob.Take(1))
.Subscribe(x =>
{
//Do something with X here.
x.Dump();
});
你需要做这样的事情:
var ob = Observable.Interval(TimeSpan.FromSeconds(1)).StartWith(500);
var latestAndThenTheRest =
Observable
.Create<long>(o =>
{
var bs = new BehaviorSubject<long>(1);
var s1 = ob.Subscribe(bs);
var s2 = bs.Subscribe(o);
return new CompositeDisposable(s1, s2);
});
在这里你唯一需要考虑的是ob
必须是一个热门的 observable 才能让它有意义。 如果天气很冷,那么每个订阅者都会在ob
序列的开头获得一个全新的订阅。
只是为了澄清这一点,并感谢@LeeCampbell 的回答。
什么不起作用:
var ob = Observable.Interval(TimeSpan.FromSeconds(1)).StartWith(500).Replay(1).RefCount();
Observable.Timer(TimeSpan.FromSeconds(5)).Subscribe(x =>
{
ob.First().Dump();
// This gives you 500.
// Because this is the first time any one subscribes to the observable,
// so it starts right here and gives you the initial value.
});
什么会真正起作用:
var ob = Observable.Interval(TimeSpan.FromSeconds(1)).StartWith(500).Replay(1).RefCount();
ob.Subscribe(); // Subscribe to start the above hot observable immediately.
Observable.Timer(TimeSpan.FromSeconds(5)).Subscribe(x =>
{
ob.First().Dump();
// This would give you either 3 or 4, depending on the speed and timing of your computer.
});
我不确定这个答案是否对你有帮助,但你有没有研究过 BehaviorSubject? 它是一个 IObservable,可以记住它的最新值。 这有点像将常规变量和可观察对象合二为一。
否则,您为什么不订阅 'ob' 并自己将最新值存储在变量中?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.