summaryrefslogtreecommitdiff
path: root/Sora/Data/ImageCacheManager.swift
diff options
context:
space:
mode:
authorFuwn <[email protected]>2025-07-11 06:32:46 -0700
committerFuwn <[email protected]>2025-07-11 06:32:46 -0700
commit2e079945cb3c68ed05750757b59f0acc59155010 (patch)
tree23901f3b5ef4956f7983bccc2922b9b9378dfcbb /Sora/Data/ImageCacheManager.swift
parentfeat: Development commit (diff)
downloadsora-testing-2e079945cb3c68ed05750757b59f0acc59155010.tar.xz
sora-testing-2e079945cb3c68ed05750757b59f0acc59155010.zip
feat: Development commit
Diffstat (limited to 'Sora/Data/ImageCacheManager.swift')
-rw-r--r--Sora/Data/ImageCacheManager.swift25
1 files changed, 13 insertions, 12 deletions
diff --git a/Sora/Data/ImageCacheManager.swift b/Sora/Data/ImageCacheManager.swift
index ed68c67..8b886df 100644
--- a/Sora/Data/ImageCacheManager.swift
+++ b/Sora/Data/ImageCacheManager.swift
@@ -1,6 +1,7 @@
import Combine
import SwiftUI
+@MainActor
final class ImageCacheManager {
// MARK: - Singleton
static let shared = ImageCacheManager()
@@ -13,9 +14,6 @@ final class ImageCacheManager {
.appendingPathComponent("SoraImageCache", isDirectory: true)
)
private var cancellables = Set<AnyCancellable>()
- private let cancellablesQueue = DispatchQueue(
- label: "\(Bundle.main.bundleIdentifier!).ImageCacheManager.cancellablesQueue"
- )
private let downloadQueue = OperationQueue()
// MARK: - Initialisation
@@ -25,16 +23,19 @@ final class ImageCacheManager {
// MARK: - Public Methods
func preloadImages(_ urls: [URL]) {
- urls.forEach { url in
+ for url in urls {
downloadQueue.addOperation {
- self.cancellablesQueue.sync {
- URLSession.shared.dataTaskPublisher(for: url)
- .map { CachedURLResponse(response: $0.response, data: $0.data) }
- .sink(
- receiveCompletion: { _ in () },
- receiveValue: { [self] in cache.storeCachedResponse($0, for: URLRequest(url: url)) }
- )
- .store(in: &self.cancellables)
+ let cancellable = URLSession.shared.dataTaskPublisher(for: url)
+ .map { CachedURLResponse(response: $0.response, data: $0.data) }
+ .sink(
+ receiveCompletion: { _ in () },
+ receiveValue: { [weak self] cachedResponse in
+ self?.cache.storeCachedResponse(cachedResponse, for: URLRequest(url: url))
+ }
+ )
+
+ DispatchQueue.main.async { [weak self] in
+ self?.cancellables.insert(cancellable)
}
}
}