Add async await version of downloadUsingCache.

This commit is contained in:
Brent Simmons 2024-04-10 19:03:23 -07:00
parent 76fffca825
commit 0a626b1904

View File

@ -183,6 +183,28 @@ private struct CallbackRecord {
}
}
public struct DownloadData: Sendable {
public let data: Data?
public let response: URLResponse?
}
@MainActor public func downloadUsingCache(_ url: URL) async throws -> DownloadData {
precondition(Thread.isMainThread)
return try await withCheckedThrowingContinuation { continuation in
downloadUsingCache(url) { data, response, error in
if let error {
continuation.resume(throwing: error)
} else {
let downloadData = DownloadData(data: data, response: response)
continuation.resume(returning: downloadData)
}
}
}
}
@MainActor public func downloadUsingCache(_ url: URL, _ completion: @escaping OneShotDownloadCallback) {
precondition(Thread.isMainThread)
DownloadWithCacheManager.shared.download(url, completion)