Procházet zdrojové kódy

Merge pull request #984 from pNre/master

Serialize access to downloadHolder
Wei Wang před 7 roky
rodič
revize
b363e045f9
1 změnil soubory, kde provedl 18 přidání a 3 odebrání
  1. 18 3
      Sources/ImageDownloader.swift

+ 18 - 3
Sources/ImageDownloader.swift

@@ -294,7 +294,7 @@ open class ImageDownloader {
         processQueue = DispatchQueue(label: "com.onevcat.Kingfisher.ImageDownloader.Process.\(name)", attributes: .concurrent)
         cancelQueue = DispatchQueue(label: "com.onevcat.Kingfisher.ImageDownloader.Cancel.\(name)")
         
-        sessionHandler = ImageDownloaderSessionHandler()
+        sessionHandler = ImageDownloaderSessionHandler(name: name)
 
         // Provide a default implement for challenge responder.
         authenticationChallengeResponder = sessionHandler
@@ -463,10 +463,25 @@ extension ImageDownloader {
 /// So we need an additional handler to break the retain cycle.
 // See https://github.com/onevcat/Kingfisher/issues/235
 final class ImageDownloaderSessionHandler: NSObject, URLSessionDataDelegate, AuthenticationChallengeResponsable {
-    
+
+    private let downloaderQueue: DispatchQueue
+
     // The holder will keep downloader not released while a data task is being executed.
     // It will be set when the task started, and reset when the task finished.
-    var downloadHolder: ImageDownloader?
+    private var _downloadHolder: ImageDownloader?
+    var downloadHolder: ImageDownloader? {
+        get {
+            return downloaderQueue.sync { _downloadHolder }
+        }
+        set {
+            downloaderQueue.sync { _downloadHolder = newValue }
+        }
+    }
+
+    init(name: String) {
+        downloaderQueue = DispatchQueue(label: "com.onevcat.Kingfisher.ImageDownloader.SessionHandler.\(name)")
+        super.init()
+    }
     
     func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: @escaping (URLSession.ResponseDisposition) -> Void) {