Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between catch: and subscribeError:

In ReactiveCocoa, what's the difference between the subscribeError: method vs. catch:? Why would you want to return a signal in catch:?

like image 739
Ash Furrow Avatar asked Oct 18 '13 00:10

Ash Furrow


People also ask

How do you throw an error in subscribe?

Example 1: Throw error on subscriptionconst source = throwError('This is an error! '); //output: 'Error: This is an error!

What is catch error in Angular?

Angular CatchError is an RxJs Operator. We can use it to handle the errors thrown by the Angular Observable. Like all other RxJs operators, the CatchError also takes an observable as input and returns an observable (or throws an error).

How do you catch errors in switchMap?

Good Error Handling Always put the “catchError” operator inside a switchMap (or similar) so that it only ends the API call stream and then returns the stream to the switchMap, which continues the Observable.


1 Answers

-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."

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:.

Examples of what you could do in -catch::

  1. Return [RACSignal empty] if you want to fail silently and not throw an error.
  2. Return [RACSignal error:err] if you want to rethrow the error after doing something, or perhaps you want to transform the error.
  3. Return some other signal that you want to subscribe to in case the first one errors out.
like image 86
Jonathan Sterling Avatar answered Oct 23 '22 03:10

Jonathan Sterling