summaryrefslogtreecommitdiff
path: root/Sora/Data/ImageCacheManager.swift
diff options
context:
space:
mode:
authorFuwn <[email protected]>2025-09-03 22:28:08 -0700
committerFuwn <[email protected]>2025-09-03 22:28:08 -0700
commite42ace815c0e33e90a6e576942d7905f01a314dd (patch)
tree4e3a8b15e6e2ebfb5436014660c990a57350e069 /Sora/Data/ImageCacheManager.swift
parentfeat: Development commit (diff)
downloadsora-testing-e42ace815c0e33e90a6e576942d7905f01a314dd.tar.xz
sora-testing-e42ace815c0e33e90a6e576942d7905f01a314dd.zip
feat: Development commit
Diffstat (limited to 'Sora/Data/ImageCacheManager.swift')
-rw-r--r--Sora/Data/ImageCacheManager.swift28
1 files changed, 27 insertions, 1 deletions
diff --git a/Sora/Data/ImageCacheManager.swift b/Sora/Data/ImageCacheManager.swift
index cd8b4e2..8a14198 100644
--- a/Sora/Data/ImageCacheManager.swift
+++ b/Sora/Data/ImageCacheManager.swift
@@ -1,5 +1,5 @@
import Combine
-import SwiftUI
+@preconcurrency import SwiftUI
@MainActor
final class ImageCacheManager {
@@ -16,11 +16,24 @@ final class ImageCacheManager {
private var cancellables = Set<AnyCancellable>()
private let downloadQueue = OperationQueue()
private var preloadingURLs = Set<URL>()
+ private var memoryWarningObserver: NSObjectProtocol?
// MARK: - Initialisation
private init() {
downloadQueue.maxConcurrentOperationCount = 5
downloadQueue.qualityOfService = .utility
+
+ #if os(iOS)
+ memoryWarningObserver = NotificationCenter.default.addObserver(
+ forName: UIApplication.didReceiveMemoryWarningNotification,
+ object: nil,
+ queue: .main
+ ) { [weak self] _ in
+ Task { @MainActor in
+ self?.handleMemoryPressure()
+ }
+ }
+ #endif
}
// MARK: - Public Methods
@@ -60,4 +73,17 @@ final class ImageCacheManager {
func getCachedResponse(for url: URL) -> CachedURLResponse? {
cache.cachedResponse(for: URLRequest(url: url))
}
+
+ private func handleMemoryPressure() {
+ cache.removeAllCachedResponses()
+ downloadQueue.cancelAllOperations()
+ cancellables.removeAll()
+ preloadingURLs.removeAll()
+ }
+
+ deinit {
+ if let observer = memoryWarningObserver {
+ NotificationCenter.default.removeObserver(observer)
+ }
+ }
}