Skip to content

Instantly share code, notes, and snippets.

@insidegui
Created March 19, 2017 17:45
Show Gist options
  • Save insidegui/8c726b4b705ddfe2fe01f24672c9a792 to your computer and use it in GitHub Desktop.
Save insidegui/8c726b4b705ddfe2fe01f24672c9a792 to your computer and use it in GitHub Desktop.
/// 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