From e1fe4b01160daa0637cc1ce5eb2c0d81cb2cbaa5 Mon Sep 17 00:00:00 2001 From: marty Date: Tue, 5 Nov 2024 01:22:45 -0800 Subject: [PATCH] fix cache --- .../Modules/Helpers/Files/FileManagerService.swift | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/iosApp/Modules/Helpers/Files/FileManagerService.swift b/iosApp/Modules/Helpers/Files/FileManagerService.swift index a3608a2c..5bcf1b6c 100644 --- a/iosApp/Modules/Helpers/Files/FileManagerService.swift +++ b/iosApp/Modules/Helpers/Files/FileManagerService.swift @@ -8,7 +8,7 @@ public class FileManagerService: ObservableObject { public typealias ProgressHandler = (Double) -> Void let downloadManager = DownloadManager() - private var downloadedFilesCache: Set = [] + private var downloadedFilesCache: [URL: Bool] = [:] @Injected private var errorLogger: ErrorReporting private var cancels: Set = [] @@ -28,12 +28,12 @@ public class FileManagerService: ObservableObject { public func fileExists(for url: URL) -> Bool { do { - guard downloadedFilesCache.contains(url) == false else { - return true + if let cached = downloadedFilesCache[url] { + return cached } let url = try fileURL(forDownloadURL: url) let fileExists = FileManager.default.fileExists(atPath: url.path) - downloadedFilesCache.insert(url) + downloadedFilesCache[url] = fileExists return fileExists } catch { errorLogger.logError(error) @@ -58,7 +58,7 @@ public class FileManagerService: ObservableObject { } public func clearFile(at url: URL) async { - downloadedFilesCache.remove(url) + downloadedFilesCache[url] = nil await Task { do { try FileManager.default.removeItem(at: try fileURL(forDownloadURL: url)) @@ -72,7 +72,7 @@ public class FileManagerService: ObservableObject { public func download(url: URL, progressHandler: @escaping ProgressHandler) async throws { progressHandler(0) try await downloadManager.download(url: url, progressHandler: progressHandler) - downloadedFilesCache.insert(url) + downloadedFilesCache[url] = true } public func cancelDownload(url: URL) {