Skip to content

Instantly share code, notes, and snippets.

@krin-san
Last active October 22, 2021 00:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krin-san/b9ffbd03d491b6945925 to your computer and use it in GitHub Desktop.
Save krin-san/b9ffbd03d491b6945925 to your computer and use it in GitHub Desktop.
ReactiveCocoa. Optional retry on error
// Taken from http://www.slideshare.net/manuelmaly/reactivecocoa-goodness-part-i-of-ii
// Exact presentation slide: http://image.slidesharecdn.com/presentation-141009165841-conversion-gate01/95/reactivecocoa-goodness-part-i-of-ii-37-638.jpg?cb=1412892113
// Try to repeat signal 3 times
static NSUInteger const retryAttempts = 2;
static NSTimeInterval const retrydelay = 1.;
RACSignal *signal = ...;
RACSignal *result = [[signal catch:^RACSignal *(NSError *error) {
return [[[RACSignal empty] delay:retrydelay]
concat:[RACSignal error:error]];
}] retry:retryAttempts];
// Improved implementation
// Supports optional breaking of retry-cycle
// Try to repeat signal 3 times
static NSUInteger const retryAttempts = 2;
static NSTimeInterval const retrydelay = 1.;
static NSUInteger attempt;
- (void)performSignal {
attempt = 0;
RACSignal *signal = ...;
RACSignal *result = [self retry:signal];
}
- (RACSignal *)retry:(RACSignal *)signal {
return [signal catch:^RACSignal *(NSError *error) {
attempt++;
if (attempt <= retryAttempts && [self allowRetryOnError:error]) {
return [[[RACSignal empty] delay:retrydelay] then:^RACSignal *{
return [self retry:signal];
}];
}
return [RACSignal error:error];
}];
}
- (BOOL)allowRetryOnError:(NSError *)error {
return ...;
}
@loplopLover
Copy link

Thanks for your sharing,
How can I make a retry operation until viewController dismiss?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment