Rx.NET 中的 Wait 运算符有什么作用? [英] What does the Wait operator in Rx.NET do?

查看:27
本文介绍了Rx.NET 中的 Wait 运算符有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Rx.NET 库的 v2.2.5 中,有一个名为 Wait 的操作符,定义如下:

In v2.2.5 of the Rx.NET library, there is an operator named Waitthat is defined as so:

public virtual TSource Wait<TSource>(IObservable<TSource> source)

类都不是MSDN 上的库参考此页面 均未提及此运算符.

Neither the class library reference on MSDN nor this page mention this operator.

从它的实现来看,它有点太繁琐了,我猜它会等待 observable 产生它的所有元素,如果 observable 有任何元素,则返回最后一个元素,如果没有,它返回默认(TSource).但我不确定.

From looking at its implementation, which is a bit too cumbersome to follow, I am guessing it waits for the observable to produce all its elements and returns the last element if the observable had any elements, and if not, it returns the default(TSource). But I am not sure.

如果这是正确的,那么它与 LastOrDefaultAsync 有什么不同?

If this is correct, then how is it different from LastOrDefaultAsync?

它实际上有什么作用?

推荐答案

intellisense 文档似乎很准确

The intellisense documentation seems pretty accurate

等待可观察序列完成并返回序列的最后一个元素.如果序列以 OnError 通知终止,则抛出异常.

Waits for the observable sequence to complete and returns the last element of the sequence. If the sequence terminates with an OnError notification, the exception is thrown.

https://github.com/Reactive-Extensions/Rx.NET/blob/master/Rx.NET/Source/System.Reactive.Linq/Reactive/Linq/Observable.Blocking.cs#L493

所以操作符将阻塞调用线程(YUCK!),直到序列完成,然后产生最后一个值.

So the operator will block the calling thread (YUCK!) until the sequence completes and then yield the last value.

LastOrDefaultAsync 返回一个 IObservable,因此不会阻塞.

LastOrDefaultAsync in contrast returns an IObservable<T> so is not blocking.

这篇关于Rx.NET 中的 Wait 运算符有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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