Skip to content

Commit

Permalink
fix cache
Browse files Browse the repository at this point in the history
  • Loading branch information
martyu committed Nov 5, 2024
1 parent 676c6f5 commit e1fe4b0
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions iosApp/Modules/Helpers/Files/FileManagerService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class FileManagerService: ObservableObject {
public typealias ProgressHandler = (Double) -> Void

let downloadManager = DownloadManager()
private var downloadedFilesCache: Set<URL> = []
private var downloadedFilesCache: [URL: Bool] = [:]

@Injected private var errorLogger: ErrorReporting
private var cancels: Set<AnyCancellable> = []
Expand All @@ -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)
Expand All @@ -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))
Expand All @@ -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) {
Expand Down

0 comments on commit e1fe4b0

Please sign in to comment.