捕获错误后可观察的可观察继续订阅

发布于 2025-02-12 10:23:28 字数 1459 浏览 0 评论 0原文

以下是我正在从事的项目中遇到的代码,并尽可能多地使用。

ViewModel在命令(按钮按)上聆听触发_subject.onnext()的命令(按钮按)。它订阅_subject如下。有一个扩展方法,oneror会捕获异常。

发生的事情是,该错误被视为预期,但可观察到的可观察到的从getObservable不断重试。因此,我会像以下不停止一样输出输出。我不明白Onerror在这里的工作方式。 catch在返回源时会自动重新订阅?


输出:

SUBSCRIBED
System.Exception: ERROR
SUBSCRIBED
System.Exception: ERROR
SUBSCRIBED
System.Exception: ERROR

代码:

public ViewModel()
{
    _subject.Subscribe(_ => Save());
}

private void Save()
{
    GetObservable().OnError(ex => Debug.WriteLine(ex)).Subscribe();
}

private IObservable<Unit> GetObservable()
{
    return Observable.Create<Unit>(observer =>
    {
        Debug.WriteLine("SUBSCRIBED");
        observer.OnNext(Unit.Default);
        observer.OnError(new Exception("ERROR"));
        observer.OnCompleted();
        return Disposable.Empty;
    }).Timeout(TimeSpan.FromSeconds(60));
}

public static class ObservableExtensions
{
    public static IObservable<T> OnError<T>(this IObservable<T> source, Action<Exception> action)
    {
        IObservable<T> observable = null;
        observable = source.Catch<T, Exception>(ex =>
          {
              action(ex);
              return observable;
          });
        return observable;
    }
}

The below is code I encountered from a project I am working on, simpified as much as I can.

The ViewModel listens on a command (button press) which triggers _subject.OnNext(). It subscribes to _subject as below. There is an extension method, OnError that catches exceptions.

What is happening is that the error is caught as expected but then the observable returned from GetObservable keeps retrying. So I get output like the below non stop. I am not understanding how OnError is working here. Does Catch automatically resubscribe when returning a source?


Output:

SUBSCRIBED
System.Exception: ERROR
SUBSCRIBED
System.Exception: ERROR
SUBSCRIBED
System.Exception: ERROR

Code:

public ViewModel()
{
    _subject.Subscribe(_ => Save());
}

private void Save()
{
    GetObservable().OnError(ex => Debug.WriteLine(ex)).Subscribe();
}

private IObservable<Unit> GetObservable()
{
    return Observable.Create<Unit>(observer =>
    {
        Debug.WriteLine("SUBSCRIBED");
        observer.OnNext(Unit.Default);
        observer.OnError(new Exception("ERROR"));
        observer.OnCompleted();
        return Disposable.Empty;
    }).Timeout(TimeSpan.FromSeconds(60));
}

public static class ObservableExtensions
{
    public static IObservable<T> OnError<T>(this IObservable<T> source, Action<Exception> action)
    {
        IObservable<T> observable = null;
        observable = source.Catch<T, Exception>(ex =>
          {
              action(ex);
              return observable;
          });
        return observable;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

权谋诡计 2025-02-19 10:23:28

catch()操作员返回可观察的可观察到的,就像“串联”它被调用的可观察到的和可观察到的可观察到的可观察到的可观察到的可观察到的可观察到的func&lt; texception,iobservable,iObservable&lt; tsource&gt;&gt;&gt;&gt;&gt;表达式有例外。从 https://reactivex.io operatortation/catch .html

”“”

如您所见,结果是“一个”。

因此,在您的情况下,您会得到:

OnNext(Unit.Default)

由于例外,您目前正在构建的可观察到。因此,基本上您再次从顶部开始。这意味着您可观察到:

OnNext(Unit.Default)
OnNext(Unit.Default)
OnNext(Unit.Default)
OnNext(Unit.Default)
OnNext(Unit.Default)
...

The Catch() operator returns an observable, which is like "concatenating" the observable it is called on and the observable which is defined in the provided Func<TException, IObservable<TSource>> expression in case there was an exception. Check the marble diagram of the Catch operator from https://reactivex.io/documentation/operators/catch.html:

As you see the result is "one" observable.

So in your case you get:

OnNext(Unit.Default)

and due to the exception the observable you are currently building. So basically you start from the top again. And this means you observable generates:

OnNext(Unit.Default)
OnNext(Unit.Default)
OnNext(Unit.Default)
OnNext(Unit.Default)
OnNext(Unit.Default)
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文