diff options
Diffstat (limited to 'Sora/Data/ImageCacheManager.swift')
| -rw-r--r-- | Sora/Data/ImageCacheManager.swift | 18 |
1 files changed, 11 insertions, 7 deletions
diff --git a/Sora/Data/ImageCacheManager.swift b/Sora/Data/ImageCacheManager.swift index 5da8614..e789b74 100644 --- a/Sora/Data/ImageCacheManager.swift +++ b/Sora/Data/ImageCacheManager.swift @@ -13,22 +13,26 @@ final class ImageCacheManager { .appendingPathComponent("SoraImageCache", isDirectory: true) ) private var cancellables = Set<AnyCancellable>() + private let downloadQueue = OperationQueue() // MARK: - Initialisation private init() { URLCache.shared = cache + downloadQueue.maxConcurrentOperationCount = 5 } // MARK: - Public Methods func preloadImages(_ urls: [URL]) { urls.forEach { url in - URLSession.shared.dataTaskPublisher(for: url) - .map { CachedURLResponse(response: $0.response, data: $0.data) } - .sink( - receiveCompletion: { _ in () }, - receiveValue: { [cache] in cache.storeCachedResponse($0, for: URLRequest(url: url)) } - ) - .store(in: &cancellables) + 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) + } } } } |