Zach Lucas
Zach Lucas

Reputation: 1641

How to resubscribe to RACSignal after error

I have a simple subscribeNext: error: chain like:

[[self.viewModel signal] subscribeNext:^(id x) {
    NSLog(@"success!");
} error:^(NSError *error) {
    NSLog(@"error");
}];

When I receive an error, I successfully print error, but it seems like the chain is never called again, even if the signal is sent from the view model again after the error. I can't seem to figure out how to use -retry or -repeat to resubscribe after the error fires. Any help? Thanks!

Upvotes: 1

Views: 769

Answers (3)

hustpy
hustpy

Reputation: 11

If you put the -catch: inside the -flattenMap:, then the outer signal won't error. https://github.com/ReactiveCocoa/ReactiveCocoa/issues/1218

Upvotes: 1

Zach Lucas
Zach Lucas

Reputation: 1641

Okay! I figured out the answer: You need to catch the error with a catch block, return a RACSignal with the error, then retry, then subscribeNext to the success callback. Like:

[[[[self.viewModel signal] catch:^RACSignal *(NSError *error) {
    // Handle the error here
    return [RACSignal error:error];
}] retry] subscribeNext:^(id x) {
    // Do your success stuff
}];

Upvotes: 3

Sonny Saluja
Sonny Saluja

Reputation: 7287

You need -[RACSignal retry]:

[[[self.viewModel signal] retry] subscribeNext:^(id x) {
    NSLog(@"success!");
} error:^(NSError *error) {
    NSLog(@"error");
}];

Upvotes: 0

Related Questions