Prechádzať zdrojové kódy

Remove unnecessary I/O queue dispatch

onevcat 7 rokov pred
rodič
commit
149d3a97ae

+ 1 - 1
Demo/Demo/Kingfisher-Demo/ViewController.swift

@@ -71,7 +71,7 @@ extension ViewController {
         let imageView = (cell as! CollectionViewCell).cellImageView!
         let imageView = (cell as! CollectionViewCell).cellImageView!
         imageView.kf.setImage(
         imageView.kf.setImage(
             with: url,
             with: url,
-            placeholder: imageView.image,
+            placeholder: nil,
             options: [.transition(.fade(1))],
             options: [.transition(.fade(1))],
             progressBlock: { receivedSize, totalSize in
             progressBlock: { receivedSize, totalSize in
                 print("\(indexPath.row + 1): \(receivedSize)/\(totalSize)")
                 print("\(indexPath.row + 1): \(receivedSize)/\(totalSize)")

+ 25 - 27
Sources/Cache/ImageCache.swift

@@ -387,34 +387,32 @@ open class ImageCache {
             callbackQueue.execute { completionHandler(.success(.none)) }
             callbackQueue.execute { completionHandler(.success(.none)) }
         } else {
         } else {
             // Begin to disk search.
             // Begin to disk search.
-            ioQueue.async {
-                self.retrieveImageInDiskCache(forKey: key, options: options, callbackQueue: callbackQueue) {
-                    result in
-                    // The callback queue is already correct in this closure.
-                    switch result {
-                    case .success(let image):
-                        guard let image = imageModifier.modify(image) else {
-                            // No image found in disk storage.
-                            completionHandler(.success(.none))
-                            return
-                        }
-                    
-                        // Cache the disk image to memory.
-                        // We are passing `false` to `toDisk`, the memory cache does not change
-                        // callback queue, we can call `completionHandler` without another dispatch.
-                        self.store(
-                            image,
-                            forKey: key,
-                            processorIdentifier: options.processor.identifier,
-                            cacheSerializer: options.cacheSerializer,
-                            toDisk: false)
-                        {
-                            _ in
-                            completionHandler(.success(.disk(image)))
-                        }
-                    case .failure(let error):
-                        completionHandler(.failure(error))
+            self.retrieveImageInDiskCache(forKey: key, options: options, callbackQueue: callbackQueue) {
+                result in
+                // The callback queue is already correct in this closure.
+                switch result {
+                case .success(let image):
+                    guard let image = imageModifier.modify(image) else {
+                        // No image found in disk storage.
+                        completionHandler(.success(.none))
+                        return
+                    }
+                
+                    // Cache the disk image to memory.
+                    // We are passing `false` to `toDisk`, the memory cache does not change
+                    // callback queue, we can call `completionHandler` without another dispatch.
+                    self.store(
+                        image,
+                        forKey: key,
+                        processorIdentifier: options.processor.identifier,
+                        cacheSerializer: options.cacheSerializer,
+                        toDisk: false)
+                    {
+                        _ in
+                        completionHandler(.success(.disk(image)))
                     }
                     }
+                case .failure(let error):
+                    completionHandler(.failure(error))
                 }
                 }
             }
             }
         }
         }

+ 9 - 3
Sources/Networking/ImagePrefetcher.swift

@@ -37,7 +37,8 @@ import UIKit
 /// - `failedResources`: An array of resources that fail to be downloaded. It could because of being cancelled while
 /// - `failedResources`: An array of resources that fail to be downloaded. It could because of being cancelled while
 ///                      downloading, encountered an error when downloading or the download not being started at all.
 ///                      downloading, encountered an error when downloading or the download not being started at all.
 /// - `completedResources`: An array of resources that are downloaded and cached successfully.
 /// - `completedResources`: An array of resources that are downloaded and cached successfully.
-public typealias PrefetcherProgressBlock = ((_ skippedResources: [Resource], _ failedResources: [Resource], _ completedResources: [Resource]) -> Void)
+public typealias PrefetcherProgressBlock =
+    ((_ skippedResources: [Resource], _ failedResources: [Resource], _ completedResources: [Resource]) -> Void)
 
 
 /// Completion block of prefetcher.
 /// Completion block of prefetcher.
 ///
 ///
@@ -45,7 +46,8 @@ public typealias PrefetcherProgressBlock = ((_ skippedResources: [Resource], _ f
 /// - `failedResources`: An array of resources that fail to be downloaded. It could because of being cancelled while
 /// - `failedResources`: An array of resources that fail to be downloaded. It could because of being cancelled while
 ///                      downloading, encountered an error when downloading or the download not being started at all.
 ///                      downloading, encountered an error when downloading or the download not being started at all.
 /// - `completedResources`: An array of resources that are downloaded and cached successfully.
 /// - `completedResources`: An array of resources that are downloaded and cached successfully.
-public typealias PrefetcherCompletionHandler = ((_ skippedResources: [Resource], _ failedResources: [Resource], _ completedResources: [Resource]) -> Void)
+public typealias PrefetcherCompletionHandler =
+    ((_ skippedResources: [Resource], _ failedResources: [Resource], _ completedResources: [Resource]) -> Void)
 
 
 /// `ImagePrefetcher` represents a downloading manager for requesting many images via URLs, then caching them.
 /// `ImagePrefetcher` represents a downloading manager for requesting many images via URLs, then caching them.
 /// This is useful when you know a list of image resources and want to download them before showing. It also works with
 /// This is useful when you know a list of image resources and want to download them before showing. It also works with
@@ -107,7 +109,11 @@ public class ImagePrefetcher {
                completionHandler: PrefetcherCompletionHandler? = nil)
                completionHandler: PrefetcherCompletionHandler? = nil)
     {
     {
         let resources: [Resource] = urls.map { $0 }
         let resources: [Resource] = urls.map { $0 }
-        self.init(resources: resources, options: options, progressBlock: progressBlock, completionHandler: completionHandler)
+        self.init(
+            resources: resources,
+            options: options,
+            progressBlock: progressBlock,
+            completionHandler: completionHandler)
     }
     }
 
 
     /// Creates an image prefetcher with an array of resources.
     /// Creates an image prefetcher with an array of resources.