Last active
October 22, 2021 00:24
-
-
Save krin-san/b9ffbd03d491b6945925 to your computer and use it in GitHub Desktop.
ReactiveCocoa. Optional retry on error
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 ...; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for your sharing,
How can I make a retry operation until viewController dismiss?