问题描述
在 ReactiveCocoa 中,subscribeError:
方法与 catch 有什么区别:代码>?为什么要在
catch:
中返回信号?
In ReactiveCocoa, what's the difference between the subscribeError:
method vs. catch:
? Why would you want to return a signal in catch:
?
推荐答案
-subscribeError:
实际订阅:这是行尾.而 -catch:
只是将信号转换为新信号(实际上并不订阅).把信号想象成一个程序.当您 -subscribeError:
时,您是在告诉计算机我想运行这个程序,但我只想在它出错时收到您的回复."当您 -catch:
时,您是在说我的这个程序可能会抛出错误,我想根据旧程序创建一个新程序,以不同方式处理该错误."
-subscribeError:
actually subscribes: this is the end of the line. Whereas -catch:
simply transforms a signal into a new signal (and doesn't actually subscribe). Think of the signal like a program. When you -subscribeError:
, you are telling the computer "I want to run this program, but I only want to hear back from you if it errors out." When you -catch:
, you are saying "I've got this program that may throw an error, and I want to make a new one based on the old one that handles that error differently."
必须在 -catch:
中返回信号的原因是它不仅仅是为了抑制错误:它实际上是为了响应错误.一旦原始信号出错,它就像消失一样好:如果你想让结果信号在失败后继续运行,你必须在 -catch:
中给出一个新信号.
The reason you have to return a signal in -catch:
is that it is not simply for squelching errors: it is actually for responding to errors. Once the original signal has errored out, it's as good as gone: if you want the resultant signal to keep going after a failure, you have to do give a new signal in -catch:
.
您可以在 -catch:
中执行的操作示例:
Examples of what you could do in -catch:
:
- 如果您想静默失败而不抛出错误,请返回
[RACSignal empty]
. - 返回
[RACSignal error:err]
如果你想在做某事后重新抛出错误,或者你想转换错误. - 返回一些您想要订阅的其他信号,以防第一个信号出错.
- Return
[RACSignal empty]
if you want to fail silently and not throw an error. - Return
[RACSignal error:err]
if you want to rethrow the error after doing something, or perhaps you want to transform the error. - Return some other signal that you want to subscribe to in case the first one errors out.
这篇关于catch: 和 subscribeError 之间的区别:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!