Skip to content

Instantly share code, notes, and snippets.

@kean
Last active April 22, 2016 08:26
Show Gist options
  • Save kean/86fc84827301f286868a921072f76eff to your computer and use it in GitHub Desktop.
Save kean/86fc84827301f286868a921072f76eff to your computer and use it in GitHub Desktop.
#import <Foundation/Foundation.h>
static const NSInteger GMAutoRetryControllerInfiniteAttemptCount = INT_MAX;
/*! Auto retry with progressive delay. Thread safe.
*/
@interface GMAutoRetryController : NSObject
@property (nullable, nonatomic, copy) void (^block)(GMAutoRetryController *__nonnull autoRetryController);
GM_UNAVAILABLE_INIT_AND_NEW
/*! Initializes auto retry controller with a block that starts some asynchronous operation that might need to be restarted on failure. Block is called with each scheduled auto retry. After you create the auto retry controller, you must start it by calling its resume method.
*/
- (nonnull instancetype)initWithBlock:(void (^__nullable)(GMAutoRetryController *__nonnull autoRetryController))block NS_DESIGNATED_INITIALIZER;
/*! Executes the operation block. May be called multiple times, block would not be executed if it's already executing. This method might be used to force fast auto retries.
*/
- (void)resume;
/*! Cancels outstanding scheduled auto retries. New auto retries may not be scheduled.
*/
- (void)cancel;
/*! Schedules auto retry. Returns YES if the auto retry was scheduled. If auto retry could no be scheduled you can still restart execution manually by calling resume method.
*/
- (BOOL)scheduleAutoRetryWithError:(nullable NSError *)error;
/*! Returns YES whether auto retry should be scheduled.
*/
- (BOOL)shouldScheduleAutoRetryWithError:(nullable NSError *)error;
// default 2
@property (nonatomic) float retryDelayIncreaseRate;
// default 64
@property (nonatomic) float maximumRetryDelay;
// default 8
@property (nonatomic) float initialRetryDelay;
// deafult GMAutoRetryControllerInfiniteAttemptCount
@property (nonatomic) NSInteger maximumAttemptCount;
@property (nonatomic, readonly) NSInteger currentAttemptCount;
@property (nullable, nonatomic, readonly) NSError *currentError;
@end
@interface GMURLAutoRetryController : GMAutoRetryController
// default YES
@property (nonatomic) BOOL allowsFastRetries;
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment