Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions wire-ios/Wire-iOS/Sources/Helpers/ImageCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,28 @@ final class ImageCache<T: AnyObject> {
var cache: NSCache<NSString, T> = NSCache()
var processingQueue = DispatchQueue(label: "ImageCacheQueue", qos: .background, attributes: [.concurrent])
var dispatchGroup: DispatchGroup = .init()

private var backgroundObserver: (any NSObjectProtocol)?

init() {
// Drop decoded images when the app is backgrounded. NSCache only evicts reliably
// while in the foreground, so without this the cached images stay resident and inflate
// the suspended app's memory footprint, making it a prime target for jetsam
// ("System Pressure" background terminations).
backgroundObserver = NotificationCenter.default.addObserver(
forName: UIApplication.didEnterBackgroundNotification,
object: nil,
queue: .main
) { [weak self] _ in
self?.cache.removeAllObjects()
}
}

deinit {
if let backgroundObserver {
NotificationCenter.default.removeObserver(backgroundObserver)
}
}
}

extension UIImage {
Expand Down
Loading