Selaa lähdekoodia

trigger cleaning expired disk cache when resign active also on macOS

banjun 7 vuotta sitten
vanhempi
commit
969c31c128
1 muutettua tiedostoa jossa 11 lisäystä ja 4 poistoa
  1. 11 4
      Sources/Cache/ImageCache.swift

+ 11 - 4
Sources/Cache/ImageCache.swift

@@ -178,25 +178,32 @@ open class ImageCache {
         self.diskStorage = diskStorage
         self.diskStorage = diskStorage
         let ioQueueName = "com.onevcat.Kingfisher.ImageCache.ioQueue.\(UUID().uuidString)"
         let ioQueueName = "com.onevcat.Kingfisher.ImageCache.ioQueue.\(UUID().uuidString)"
         ioQueue = DispatchQueue(label: ioQueueName)
         ioQueue = DispatchQueue(label: ioQueueName)
-        
+
+        let notifications: [(Notification.Name, Selector)]
         #if !os(macOS) && !os(watchOS)
         #if !os(macOS) && !os(watchOS)
         #if swift(>=4.2)
         #if swift(>=4.2)
-        let notifications: [(Notification.Name, Selector)] = [
+        notifications = [
             (UIApplication.didReceiveMemoryWarningNotification, #selector(clearMemoryCache)),
             (UIApplication.didReceiveMemoryWarningNotification, #selector(clearMemoryCache)),
             (UIApplication.willTerminateNotification, #selector(cleanExpiredDiskCache)),
             (UIApplication.willTerminateNotification, #selector(cleanExpiredDiskCache)),
             (UIApplication.didEnterBackgroundNotification, #selector(backgroundCleanExpiredDiskCache))
             (UIApplication.didEnterBackgroundNotification, #selector(backgroundCleanExpiredDiskCache))
         ]
         ]
         #else
         #else
-        let notifications: [(Notification.Name, Selector)] = [
+        notifications = [
             (NSNotification.Name.UIApplicationDidReceiveMemoryWarning, #selector(clearMemoryCache)),
             (NSNotification.Name.UIApplicationDidReceiveMemoryWarning, #selector(clearMemoryCache)),
             (NSNotification.Name.UIApplicationWillTerminate, #selector(cleanExpiredDiskCache)),
             (NSNotification.Name.UIApplicationWillTerminate, #selector(cleanExpiredDiskCache)),
             (NSNotification.Name.UIApplicationDidEnterBackground, #selector(backgroundCleanExpiredDiskCache))
             (NSNotification.Name.UIApplicationDidEnterBackground, #selector(backgroundCleanExpiredDiskCache))
         ]
         ]
         #endif
         #endif
+        #elseif os(macOS)
+        notifications = [
+            (NSApplication.willResignActiveNotification, #selector(cleanExpiredDiskCache)),
+        ]
+        #else
+        notifications = []
+        #endif
         notifications.forEach {
         notifications.forEach {
             NotificationCenter.default.addObserver(self, selector: $0.1, name: $0.0, object: nil)
             NotificationCenter.default.addObserver(self, selector: $0.1, name: $0.0, object: nil)
         }
         }
-        #endif
     }
     }
     
     
     /// Creates an `ImageCache` with a given `name`. Both `MemoryStorage` and `DiskStorage` will be created
     /// Creates an `ImageCache` with a given `name`. Both `MemoryStorage` and `DiskStorage` will be created