Ensure a previous pending download is resumed

(and avoid adding a second duplicate task)
pull/28/head
Pedro Cuenca 2 years ago
parent a0297f8c3a
commit 2a549b5221

@ -34,11 +34,13 @@ class Downloader: NSObject, ObservableObject {
urlSession = URLSession(configuration: config, delegate: self, delegateQueue: OperationQueue()) urlSession = URLSession(configuration: config, delegate: self, delegateQueue: OperationQueue())
downloadState.value = .downloading(0) downloadState.value = .downloading(0)
urlSession?.getAllTasks { tasks in urlSession?.getAllTasks { tasks in
// If there's an existing pending background task, let it proceed, otherwise start a new one. // If there's an existing pending background task with the same URL, let it proceed.
// TODO: check URL when we support downloading more models. guard tasks.filter({ $0.originalRequest?.url == url }).isEmpty else {
if tasks.first == nil { print("Already downloading \(url)")
self.urlSession?.downloadTask(with: url).resume() return
} }
print("Starting download of \(url)")
self.urlSession?.downloadTask(with: url).resume()
} }
} }

Loading…
Cancel
Save