捕获错误后可观察的可观察继续订阅
以下是我正在从事的项目中遇到的代码,并尽可能多地使用。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
catch()
操作员返回可观察的可观察到的,就像“串联”它被调用的可观察到的和可观察到的可观察到的可观察到的可观察到的可观察到的可观察到的func&lt; texception,iobservable,iObservable&lt; tsource&gt;&gt;&gt;&gt;&gt;
表达式有例外。从 https://reactivex.io operatortation/catch .html :如您所见,结果是“一个”。
因此,在您的情况下,您会得到:
由于例外,您目前正在构建的可观察到。因此,基本上您再次从顶部开始。这意味着您可观察到:
The
Catch()
operator returns an observable, which is like "concatenating" the observable it is called on and the observable which is defined in the providedFunc<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:
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: