From 2a549b5221f3662217f98b59da06cda5a2894f2e Mon Sep 17 00:00:00 2001 From: Pedro Cuenca Date: Tue, 7 Feb 2023 16:06:10 +0100 Subject: [PATCH] Ensure a previous pending download is resumed (and avoid adding a second duplicate task) --- Diffusion/Downloader.swift | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Diffusion/Downloader.swift b/Diffusion/Downloader.swift index 847e34a..8bb8e4c 100644 --- a/Diffusion/Downloader.swift +++ b/Diffusion/Downloader.swift @@ -34,11 +34,13 @@ class Downloader: NSObject, ObservableObject { urlSession = URLSession(configuration: config, delegate: self, delegateQueue: OperationQueue()) downloadState.value = .downloading(0) urlSession?.getAllTasks { tasks in - // If there's an existing pending background task, let it proceed, otherwise start a new one. - // TODO: check URL when we support downloading more models. - if tasks.first == nil { - self.urlSession?.downloadTask(with: url).resume() + // If there's an existing pending background task with the same URL, let it proceed. + guard tasks.filter({ $0.originalRequest?.url == url }).isEmpty else { + print("Already downloading \(url)") + return } + print("Starting download of \(url)") + self.urlSession?.downloadTask(with: url).resume() } }