Created
February 1, 2018 11:24
-
-
Save amosavian/bcc9aa94a9e0dd41bc404ccacad3dd79 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
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) { | |
if let response = task.response as? HTTPURLResponse, response.statusCode >= 400 { | |
return | |
} | |
// Workaround a CFNetwork bug which cause a write posix error. | |
// Invalidates session and start tasks again. | |
if let error = error as? POSIXError, error.code == .ENOENT { | |
session.invalidateAndCancel() | |
return | |
} | |
// Manage Errors | |
if let error = error as? URLError, let dlItem = dlItem { | |
let resumeData = error.userInfo[NSURLSessionDownloadTaskResumeData] as? Data | |
let errorReasonNum = error.userInfo[NSURLErrorBackgroundTaskCancelledReasonKey] as? Int ?? Int.min | |
let cancelledManually = error.code == .cancelled && errorReasonNum == Int.min | |
let forceQuit = error.code == .cancelled && errorReasonNum == NSURLErrorCancelledReasonUserForceQuitApplication | |
if !cancelledManually, let resumeData = resumeData { | |
// Save data | |
} | |
} | |
func urlSession(_ session: URLSession, didBecomeInvalidWithError error: Error?) { | |
print("Invalidated session") | |
// Reinstall session | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment