Ver código fonte

Add option to load disk file synchronously

onevcat 7 anos atrás
pai
commit
763531a4f8

+ 2 - 1
Sources/Cache/ImageCache.swift

@@ -453,7 +453,8 @@ open class ImageCache {
     {
         let options = options ?? .empty
         let computedKey = key.computedKey(with: options.processor.identifier)
-        ioQueue.async {
+        let loadingQueue: CallbackQueue = options.loadDiskFileSynchronously ? .untouch : .dispatch(ioQueue)
+        loadingQueue.execute {
             do {
                 var image: Image? = nil
                 if let data = try self.diskStorage.value(forKey: computedKey) {

+ 7 - 0
Sources/General/KingfisherOptionsInfo.swift

@@ -187,6 +187,8 @@ public enum KingfisherOptionsInfoItem {
     /// aggressively. By default this is not contained in the options, that means if the requested image is already
     /// in disk cache, Kingfisher will not try to load it to memory.
     case alsoPrefetchToMemory
+    
+    case loadDiskFileSynchronously
 }
 
 precedencegroup ItemComparisonPrecedence {
@@ -227,6 +229,7 @@ func <== (lhs: KingfisherOptionsInfoItem, rhs: KingfisherOptionsInfoItem) -> Boo
     case (.cacheOriginalImage, .cacheOriginalImage): return true
     case (.onFailureImage, .onFailureImage): return true
     case (.alsoPrefetchToMemory, .alsoPrefetchToMemory): return true
+    case (.loadDiskFileSynchronously, .loadDiskFileSynchronously): return true
     default: return false
     }
 }
@@ -428,4 +431,8 @@ public extension Collection where Iterator.Element == KingfisherOptionsInfoItem
     public var alsoPrefetchToMemory: Bool {
         return contains { $0 <== .alsoPrefetchToMemory }
     }
+    
+    public var loadDiskFileSynchronously: Bool {
+        return contains { $0 <== .loadDiskFileSynchronously }
+    }
 }