Skip to content

Instantly share code, notes, and snippets.

@jacobsapps
Created April 17, 2025 08:54
Show Gist options
  • Save jacobsapps/5af8f146aab2ca54aaa96f1262757818 to your computer and use it in GitHub Desktop.
Save jacobsapps/5af8f146aab2ca54aaa96f1262757818 to your computer and use it in GitHub Desktop.
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