我正在尝试使用 SwiftUI 组合一个杂志浏览应用程序,但在尝试创建下载进度指示器时遇到了障碍。我似乎无法找到一种方法来使ProgressBar我从urlSession(didWriteData)in进行更新URLSessionDownloadDelegate。
我有一个DownloadManager作为环境对象传递给主视图的单曲,它处理所有杂志问题的下载。
class DownloadManager: NSObject, URLSessionDownloadDelegate, ObservableObject {
var activeDownloads: [URL: Download] = [:]
lazy var downloadSession: URLSession = {
let config = URLSessionConfiguration.default
return URLSession(configuration: config, delegate: self, delegateQueue: nil)
}()
/*downloading and saving the issues is handled here*/
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
guard
let url = downloadTask.originalRequest?.url,
let download = activeDownloads[url]
else {
return
}
//this is the …Run Code Online (Sandbox Code Playgroud)