瀏覽代碼

Merge pull request #1658 from wangkejie/master-extral

通过SessionDelegate子类实现URLSessionDataDelegate更多的协议
Wei Wang 4 年之前
父節點
當前提交
1694b28894

+ 7 - 1
Sources/Networking/ImageDownloader.swift

@@ -127,6 +127,13 @@ open class ImageDownloader {
             session = URLSession(configuration: sessionConfiguration, delegate: sessionDelegate, delegateQueue: nil)
             session = URLSession(configuration: sessionConfiguration, delegate: sessionDelegate, delegateQueue: nil)
         }
         }
     }
     }
+    open var sessionDelegate: SessionDelegate {
+        didSet {
+            session.invalidateAndCancel()
+            session = URLSession(configuration: sessionConfiguration, delegate: sessionDelegate, delegateQueue: nil)
+            setupSessionHandler()
+        }
+    }
     
     
     /// Whether the download requests should use pipeline or not. Default is false.
     /// Whether the download requests should use pipeline or not. Default is false.
     open var requestsUsePipelining = false
     open var requestsUsePipelining = false
@@ -139,7 +146,6 @@ open class ImageDownloader {
     open weak var authenticationChallengeResponder: AuthenticationChallengeResponsable?
     open weak var authenticationChallengeResponder: AuthenticationChallengeResponsable?
 
 
     private let name: String
     private let name: String
-    private let sessionDelegate: SessionDelegate
     private var session: URLSession
     private var session: URLSession
 
 
     // MARK: Initializers
     // MARK: Initializers

+ 7 - 7
Sources/Networking/SessionDelegate.swift

@@ -28,7 +28,7 @@ import Foundation
 
 
 // Represents the delegate object of downloader session. It also behave like a task manager for downloading.
 // Represents the delegate object of downloader session. It also behave like a task manager for downloading.
 @objc(KFSessionDelegate) // Fix for ObjC header name conflicting. https://github.com/onevcat/Kingfisher/issues/1530
 @objc(KFSessionDelegate) // Fix for ObjC header name conflicting. https://github.com/onevcat/Kingfisher/issues/1530
-class SessionDelegate: NSObject {
+open class SessionDelegate: NSObject {
 
 
     typealias SessionChallengeFunc = (
     typealias SessionChallengeFunc = (
         URLSession,
         URLSession,
@@ -149,7 +149,7 @@ class SessionDelegate: NSObject {
 
 
 extension SessionDelegate: URLSessionDataDelegate {
 extension SessionDelegate: URLSessionDataDelegate {
 
 
-    func urlSession(
+    open func urlSession(
         _ session: URLSession,
         _ session: URLSession,
         dataTask: URLSessionDataTask,
         dataTask: URLSessionDataTask,
         didReceive response: URLResponse,
         didReceive response: URLResponse,
@@ -172,7 +172,7 @@ extension SessionDelegate: URLSessionDataDelegate {
         completionHandler(.allow)
         completionHandler(.allow)
     }
     }
 
 
-    func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
+    open func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
         guard let task = self.task(for: dataTask) else {
         guard let task = self.task(for: dataTask) else {
             return
             return
         }
         }
@@ -186,7 +186,7 @@ extension SessionDelegate: URLSessionDataDelegate {
         }
         }
     }
     }
 
 
-    func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
+    open func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
         guard let sessionTask = self.task(for: task) else { return }
         guard let sessionTask = self.task(for: task) else { return }
 
 
         if let url = sessionTask.originalURL {
         if let url = sessionTask.originalURL {
@@ -214,7 +214,7 @@ extension SessionDelegate: URLSessionDataDelegate {
         onCompleted(task: task, result: result)
         onCompleted(task: task, result: result)
     }
     }
 
 
-    func urlSession(
+    open func urlSession(
         _ session: URLSession,
         _ session: URLSession,
         didReceive challenge: URLAuthenticationChallenge,
         didReceive challenge: URLAuthenticationChallenge,
         completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void)
         completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void)
@@ -222,7 +222,7 @@ extension SessionDelegate: URLSessionDataDelegate {
         onReceiveSessionChallenge.call((session, challenge, completionHandler))
         onReceiveSessionChallenge.call((session, challenge, completionHandler))
     }
     }
 
 
-    func urlSession(
+    open func urlSession(
         _ session: URLSession,
         _ session: URLSession,
         task: URLSessionTask,
         task: URLSessionTask,
         didReceive challenge: URLAuthenticationChallenge,
         didReceive challenge: URLAuthenticationChallenge,
@@ -231,7 +231,7 @@ extension SessionDelegate: URLSessionDataDelegate {
         onReceiveSessionTaskChallenge.call((session, task, challenge, completionHandler))
         onReceiveSessionTaskChallenge.call((session, task, challenge, completionHandler))
     }
     }
     
     
-    func urlSession(
+    open func urlSession(
         _ session: URLSession,
         _ session: URLSession,
         task: URLSessionTask,
         task: URLSessionTask,
         willPerformHTTPRedirection response: HTTPURLResponse,
         willPerformHTTPRedirection response: HTTPURLResponse,

+ 22 - 0
Tests/KingfisherTests/ImageDownloaderTests.swift

@@ -547,6 +547,28 @@ class ImageDownloaderTests: XCTestCase {
         XCTAssertEqual(task?.sessionTask.task.priority, URLSessionTask.highPriority)
         XCTAssertEqual(task?.sessionTask.task.priority, URLSessionTask.highPriority)
         waitForExpectations(timeout: 3, handler: nil)
         waitForExpectations(timeout: 3, handler: nil)
     }
     }
+    
+    
+    func testSessionDelegate() {
+        class ExtensionDelegate:SessionDelegate {
+            //'exp' only for test
+            public let exp:XCTestExpectation
+            init(_ expectation:XCTestExpectation) {
+                exp = expectation
+            }
+            func urlSession(_ session: URLSession, task: URLSessionTask, didFinishCollecting metrics: URLSessionTaskMetrics) {
+                exp.fulfill()
+            }
+        }
+        downloader.sessionDelegate = ExtensionDelegate(expectation(description: #function))
+        
+        let url = testURLs[0]
+        stub(url, data: testImageData)
+        downloader.downloadImage(with: url) { result in
+            XCTAssertNotNil(result.value)
+        }
+        waitForExpectations(timeout: 3, handler: nil)
+    }
 }
 }
 
 
 extension ImageDownloaderTests: ImageDownloaderDelegate {
 extension ImageDownloaderTests: ImageDownloaderDelegate {