Quellcode durchsuchen

Update queue name

 for multi-thread debug purpose. Add check for cache and downloader name when init.
onevcat vor 10 Jahren
Ursprung
Commit
19fd5596de
2 geänderte Dateien mit 35 neuen und 10 gelöschten Zeilen
  1. 13 5
      Kingfisher/ImageCache.swift
  2. 22 5
      Kingfisher/ImageDownloader.swift

+ 13 - 5
Kingfisher/ImageCache.swift

@@ -28,8 +28,8 @@ import Foundation
 
 private let defaultCacheName = "default"
 private let cacheReverseDNS = "com.onevcat.Kingfisher.ImageCache."
-private let ioQueueName = "com.onevcat.Kingfisher.ImageCache.ioQueue"
-private let processQueueName = "com.onevcat.Kingfisher.ImageCache.processQueue"
+private let ioQueueName = "com.onevcat.Kingfisher.ImageCache.ioQueue."
+private let processQueueName = "com.onevcat.Kingfisher.ImageCache.processQueue."
 
 private let defaultCacheInstance = ImageCache(name: defaultCacheName)
 private let defaultMaxCachePeriodInSecond: NSTimeInterval = 60 * 60 * 24 * 7 //Cache exists for 1 week
@@ -59,7 +59,7 @@ public class ImageCache {
     }
     
     //Disk
-    private let ioQueue = dispatch_queue_create(ioQueueName, DISPATCH_QUEUE_SERIAL)
+    private let ioQueue: dispatch_queue_t
     private let diskCachePath: String
     private var fileManager: NSFileManager!
     
@@ -69,7 +69,7 @@ public class ImageCache {
     /// The largest disk size can be taken for the cache. It is the total allocated size of cached files in bytes. Default is 0, which means no limit.
     public var maxDiskCacheSize: UInt = 0
     
-    private let processQueue = dispatch_queue_create(processQueueName, DISPATCH_QUEUE_CONCURRENT)
+    private let processQueue: dispatch_queue_t
     
     /// The default cache.
     public class var defaultCache: ImageCache {
@@ -79,17 +79,25 @@ public class ImageCache {
     /**
     Init method. Passing a name for the cache. It represents a cache folder in the memory and disk.
     
-    :param: name Name of the cache.
+    :param: name Name of the cache. It will be used as the memory cache name and the disk cache folder name. This value should not be an empty string.
     
     :returns: The cache object.
     */
     public init(name: String) {
+        
+        if name.isEmpty {
+            fatalError("[Kingfisher] You should specify a name for the cache. A cache with empty name is not permitted.")
+        }
+        
         let cacheName = cacheReverseDNS + name
         memoryCache.name = cacheName
         
         let paths = NSSearchPathForDirectoriesInDomains(.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true)
         diskCachePath = paths.first!.stringByAppendingPathComponent(cacheName)
         
+        ioQueue = dispatch_queue_create(ioQueueName + name, DISPATCH_QUEUE_SERIAL)
+        processQueue = dispatch_queue_create(processQueueName + name, DISPATCH_QUEUE_CONCURRENT)
+        
         dispatch_sync(ioQueue, { () -> Void in
             self.fileManager = NSFileManager()
         })

+ 22 - 5
Kingfisher/ImageDownloader.swift

@@ -31,9 +31,10 @@ public typealias ImageDownloaderCompletionHandler = CompletionHandler
 
 public typealias RetrieveImageDownloadTask = NSURLSessionDataTask
 
-private let downloaderBarrierName = "com.onevcat.Kingfisher.ImageDownloader.Barrier"
-private let imageProcessQueueName = "com.onevcat.Kingfisher.ImageDownloader.Process"
-private let instance = ImageDownloader()
+private let defaultDownloaderName = "default"
+private let downloaderBarrierName = "com.onevcat.Kingfisher.ImageDownloader.Barrier."
+private let imageProcessQueueName = "com.onevcat.Kingfisher.ImageDownloader.Process."
+private let instance = ImageDownloader(name: defaultDownloaderName)
 
 public class ImageDownloader: NSObject {
     
@@ -55,8 +56,8 @@ public class ImageDownloader: NSObject {
     public var trustedHosts: Set<String>?
     
     // MARK: - Internal property
-    let barrierQueue = dispatch_queue_create(downloaderBarrierName, DISPATCH_QUEUE_CONCURRENT)
-    let processQueue = dispatch_queue_create(imageProcessQueueName, DISPATCH_QUEUE_CONCURRENT)
+    let barrierQueue: dispatch_queue_t
+    let processQueue: dispatch_queue_t
     
     typealias CallbackPair = (progressBlock: ImageDownloaderProgressBlock?, completionHander: ImageDownloaderCompletionHandler?)
     
@@ -67,6 +68,22 @@ public class ImageDownloader: NSObject {
     public class var defaultDownloader: ImageDownloader {
         return instance
     }
+    
+    /**
+    Init a downloader with name.
+    
+    :param: name The name for the downloader. It should not be empty.
+    
+    :returns: The downloader object.
+    */
+    public init(name: String) {
+        if name.isEmpty {
+            fatalError("[Kingfisher] You should specify a name for the downloader. A downloader with empty name is not permitted.")
+        }
+        
+        barrierQueue = dispatch_queue_create(downloaderBarrierName + name, DISPATCH_QUEUE_CONCURRENT)
+        processQueue = dispatch_queue_create(imageProcessQueueName + name, DISPATCH_QUEUE_CONCURRENT)
+    }
 }
 
 // MARK: - Download method