-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add async download for older system versions
- Loading branch information
Showing
2 changed files
with
34 additions
and
1 deletion.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// | ||
// URLSession+AsyncDownload.swift | ||
// | ||
// | ||
// Created by Ulaş Sancak on 8.05.2024. | ||
// | ||
|
||
import Foundation | ||
|
||
/// A set of extensions for `URLSession` that provide asynchronous download capabilities. | ||
extension URLSession { | ||
/// Asynchronously downloads a file for a given `URLRequest`. | ||
/// | ||
/// - Parameters: | ||
/// - request: The `URLRequest` to be downloaded. | ||
/// - Returns: A tuple containing the downloaded file's URL and its associated URL response. | ||
/// - Throws: Any errors encountered during the download operation. | ||
func download(for request: URLRequest) async throws -> (URL, URLResponse) { | ||
return try await withCheckedThrowingContinuation { continuation in | ||
downloadTask(with: request) { url, response, error in | ||
guard let url, let response else { | ||
if let error { | ||
continuation.resume(throwing: error) | ||
} else { | ||
continuation.resume(throwing: RestingError.unknown) | ||
} | ||
return | ||
} | ||
continuation.resume(returning: (url, response)) | ||
} | ||
} | ||
} | ||
} |