简体   繁体   English

C# Rx Observable.Never<> 表现得像 Observable.Empty<>?

[英]C# Rx Observable.Never<> behaves like Observable.Empty<>?

I'm new to Rx and have this code snippet for a try.我是 Rx 的新手,并尝试使用此代码片段。

Observable.Never<string>().Subscribe(Console.Write);
Observable.Empty<string>().Subscribe(Console.Write);

I expected that Never<string>() will behave like Console.ReadKey which will not end, but as I run these 2 lines, they end immediately, so [Never] behaves like [Empty] to me.我预计Never<string>()行为会像Console.ReadKey一样不会结束,但是当我运行这两行时,它们会立即结束,所以 [Never] 对我来说就像 [Empty] 一样。

What is the correct understanding of [Never] and is there a good sample usage for it? [Never] 的正确理解是什么,是否有很好的示例用法?

Both the Observable.Never() and Observable.Empty() observable will not emit any values. Observable.Never()Observable.Empty() observable 都不会发出任何值。 However, the observable built with Observable.Never() will not complete and instead stays "open/active".但是,使用Observable.Never()构建的 observable 不会完成,而是保持“打开/活动”状态。 It might be a difference at the location where you consume these observable if the observable completes ( Empty() ) or not ( Never() ), but this depends on your actual use-case.如果 observable 完成( Empty() )或不完成( Never() ),在您使用这些 observable 的位置可能会有所不同,但这取决于您的实际用例。

Having observables which doesn't emit any values might sound useless, but maybe you are at a location where you have to provide an observable (instead of using null ).拥有不发出任何值的 observable 听起来可能毫无用处,但也许您正处于必须提供 observable 的位置(而不是使用null )。 So you can write something like this:所以你可以这样写:

public override IObservable<string> NameChanged => Observable.Never<string>();

So I don't have a ton of experience with Rx, but I believe all Subscribe is doing is registering what to do when the observable emits.所以我对 Rx 没有太多的经验,但我相信Subscribe所做的就是注册当 observable 发出时要做什么。 If your observable never emits (ie Empty or Never ) then the method is never called.如果您的 observable 永远不会发出(即EmptyNever ),则永远不会调用该方法。 The application is not waiting for the subscription itself to end.应用程序不会等待订阅本身结束。 If you wanted to wait forever you would use something like如果你想永远等待,你会使用类似的东西

Observable.Never<string>().Wait();

This ties back into the reason you should not use async operation in Subscribe .这与您不应该在Subscribe使用异步操作的原因有关。 Take the following code取以下代码

static void Main(string[] args)
{
  Observable.Range(1, 5).Subscribe(async x => await DoTheThing(x));
  Console.WriteLine("done");
}

static async Task DoTheThing(int x)
{
  await Task.Delay(TimeSpan.FromSeconds(x));
  Console.WriteLine(x);
}

When run the application will immediately write "done" and exit after pushing the values into the observable because it is unaware of the subscriber in the context of whether it has completed its handling or not.运行时,应用程序将在将值推送到 observable 后立即写入“完成”并退出,因为它不知道订阅者是否已完成其处理。 Hopefully I made that clear, and if someone with more Rx knowledge wants to step in to help if needed that'd be good.希望我说清楚了,如果有更多 Rx 知识的人想在需要时介入以提供帮助,那就太好了。

此链接给你空的从来没有,也的区别: http://reactivex.io/documentation/operators/empty-never-throw.html这是从来没有的一个用法: HTTPS://rxjs-dev.firebaseapp .com/api/index/const/从不

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM