Created
March 19, 2017 17:45
-
-
Save insidegui/8c726b4b705ddfe2fe01f24672c9a792 to your computer and use it in GitHub Desktop.
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
/// Helper method to retry a CloudKit operation when its error suggests it | |
/// | |
/// - Parameters: | |
/// - error: The error returned from a CloudKit operation | |
/// - block: A block to be executed after a delay if the error is recoverable | |
/// - Returns: If the error can't be retried, returns the error | |
func retryCloudKitOperationIfPossible(with error: Error?, block: @escaping () -> ()) -> Error? { | |
guard let effectiveError = error as? CKError else { | |
// not a CloudKit error or no error present, just return the original error | |
return error | |
} | |
guard let retryAfter = effectiveError.retryAfterSeconds else { | |
// CloudKit error, can't be retried, return the error | |
return effectiveError | |
} | |
// CloudKit operation can be retried, schedule `block` to be executed later | |
DispatchQueue.main.asyncAfter(deadline: .now() + retryAfter) { | |
block() | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment