diff options
Diffstat (limited to 'Sora/Data/ImageCacheManager.swift')
| -rw-r--r-- | Sora/Data/ImageCacheManager.swift | 20 |
1 files changed, 12 insertions, 8 deletions
diff --git a/Sora/Data/ImageCacheManager.swift b/Sora/Data/ImageCacheManager.swift index e789b74..ed68c67 100644 --- a/Sora/Data/ImageCacheManager.swift +++ b/Sora/Data/ImageCacheManager.swift @@ -13,11 +13,13 @@ 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 private init() { - URLCache.shared = cache downloadQueue.maxConcurrentOperationCount = 5 } @@ -25,13 +27,15 @@ final class ImageCacheManager { func preloadImages(_ urls: [URL]) { urls.forEach { url in downloadQueue.addOperation { - 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) + 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) + } } } } |