Created
April 17, 2025 08:54
-
-
Save jacobsapps/5af8f146aab2ca54aaa96f1262757818 to your computer and use it in GitHub Desktop.
This file contains 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
private var continuation: | |
AsyncThrowingStream<Double, Error>.Continuation? | |
func urlSession(_ session: URLSession, | |
downloadTask: URLSessionDownloadTask, | |
didWriteData bytesWritten: Int64, | |
totalBytesWritten: Int64, | |
totalBytesExpectedToWrite: Int64) { | |
guard let continuation else { return } | |
if totalBytesExpectedToWrite > 0 { | |
let progress = Double(totalBytesWritten) / Double(totalBytesExpectedToWrite) * 100 | |
continuation.yield(progress) | |
} | |
} | |
func urlSession(_ session: URLSession, | |
downloadTask: URLSessionDownloadTask, | |
didFinishDownloadingTo location: URL) { | |
continuation?.finish() | |
} | |
func urlSession(_ session: URLSession, | |
task: URLSessionTask, | |
didCompleteWithError error: Error?) { | |
if let error { | |
continuation?.yield(with: .failure(error)) | |
} | |
continuation?.finish() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment