瀏覽代碼

Updated task delegate override closures and delegate method implementations.

The task delegate override closure properties now match the formatting used in the session delegate. All closure signatures have been updated to match their method counterparts. Finally, all task delegate method implementations are now being overridden by their respective override closure.
Christian Noon 10 年之前
父節點
當前提交
9687aae410
共有 4 個文件被更改,包括 87 次插入47 次删除
  1. 23 14
      Source/Download.swift
  2. 1 1
      Source/Manager.swift
  3. 48 27
      Source/Request.swift
  4. 15 5
      Source/Upload.swift

+ 23 - 14
Source/Download.swift

@@ -133,24 +133,29 @@ extension Request {
     // MARK: - DownloadTaskDelegate
 
     class DownloadTaskDelegate: TaskDelegate, NSURLSessionDownloadDelegate {
-        var downloadTask: NSURLSessionDownloadTask! { return task as! NSURLSessionDownloadTask }
+        var downloadTask: NSURLSessionDownloadTask? { return task as? NSURLSessionDownloadTask }
         var downloadProgress: ((Int64, Int64, Int64) -> Void)?
 
         var resumeData: NSData?
         override var data: NSData? { return resumeData }
 
-        var downloadTaskDidFinishDownloadingToURL: ((NSURLSession!, NSURLSessionDownloadTask!, NSURL) -> (NSURL))?
-        var downloadTaskDidWriteData: ((NSURLSession!, NSURLSessionDownloadTask!, Int64, Int64, Int64) -> Void)?
-        var downloadTaskDidResumeAtOffset: ((NSURLSession!, NSURLSessionDownloadTask!, Int64, Int64) -> Void)?
-
         // MARK: - NSURLSessionDownloadDelegate
 
+        // MARK: Override Closures
+
+        var downloadTaskDidFinishDownloadingToURL: ((NSURLSession, NSURLSessionDownloadTask, NSURL) -> (NSURL))?
+        var downloadTaskDidWriteData: ((NSURLSession, NSURLSessionDownloadTask, Int64, Int64, Int64) -> Void)?
+        var downloadTaskDidResumeAtOffset: ((NSURLSession, NSURLSessionDownloadTask, Int64, Int64) -> Void)?
+
+        // MARK: Delegate Methods
+
         func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL) {
             if downloadTaskDidFinishDownloadingToURL != nil {
                 let destination = downloadTaskDidFinishDownloadingToURL!(session, downloadTask, location)
                 var fileManagerError: NSError?
 
                 NSFileManager.defaultManager().moveItemAtURL(location, toURL: destination, error: &fileManagerError)
+
                 if fileManagerError != nil {
                     error = fileManagerError
                 }
@@ -158,19 +163,23 @@ extension Request {
         }
 
         func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
-            progress.totalUnitCount = totalBytesExpectedToWrite
-            progress.completedUnitCount = totalBytesWritten
+            if downloadTaskDidWriteData != nil {
+                downloadTaskDidWriteData!(session, downloadTask, bytesWritten, totalBytesWritten, totalBytesExpectedToWrite)
+            } else {
+                progress.totalUnitCount = totalBytesExpectedToWrite
+                progress.completedUnitCount = totalBytesWritten
 
-            downloadTaskDidWriteData?(session, downloadTask, bytesWritten, totalBytesWritten, totalBytesExpectedToWrite)
-
-            downloadProgress?(bytesWritten, totalBytesWritten, totalBytesExpectedToWrite)
+                downloadProgress?(bytesWritten, totalBytesWritten, totalBytesExpectedToWrite)
+            }
         }
 
         func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didResumeAtOffset fileOffset: Int64, expectedTotalBytes: Int64) {
-            progress.totalUnitCount = expectedTotalBytes
-            progress.completedUnitCount = fileOffset
-
-            downloadTaskDidResumeAtOffset?(session, downloadTask, fileOffset, expectedTotalBytes)
+            if downloadTaskDidResumeAtOffset != nil {
+                downloadTaskDidResumeAtOffset!(session, downloadTask, fileOffset, expectedTotalBytes)
+            } else {
+                progress.totalUnitCount = expectedTotalBytes
+                progress.completedUnitCount = fileOffset
+            }
         }
     }
 }

+ 1 - 1
Source/Manager.swift

@@ -222,7 +222,7 @@ public class Manager {
         // MARK: Override Closures
 
         /// Overrides default behavior for NSURLSessionTaskDelegate method `URLSession:task:willPerformHTTPRedirection:newRequest:completionHandler:`.
-        public var taskWillPerformHTTPRedirection: ((NSURLSession, NSURLSessionTask, NSHTTPURLResponse, NSURLRequest) -> (NSURLRequest!))?
+        public var taskWillPerformHTTPRedirection: ((NSURLSession, NSURLSessionTask, NSHTTPURLResponse, NSURLRequest) -> (NSURLRequest?))?
 
         /// Overrides default behavior for NSURLSessionTaskDelegate method `URLSession:task:didReceiveChallenge:completionHandler:`.
         public var taskDidReceiveChallenge: ((NSURLSession, NSURLSessionTask, NSURLAuthenticationChallenge) -> (NSURLSessionAuthChallengeDisposition, NSURLCredential!))?

+ 48 - 27
Source/Request.swift

@@ -186,8 +186,10 @@ public class Request {
         Cancels the request.
     */
     public func cancel() {
-        if let downloadDelegate = delegate as? DownloadTaskDelegate {
-            downloadDelegate.downloadTask.cancelByProducingResumeData { (data) in
+        if let downloadDelegate = delegate as? DownloadTaskDelegate,
+            downloadTask = downloadDelegate.downloadTask
+        {
+            downloadTask.cancelByProducingResumeData { data in
                 downloadDelegate.resumeData = data
             }
         } else {
@@ -207,11 +209,6 @@ public class Request {
 
         var credential: NSURLCredential?
 
-        var taskWillPerformHTTPRedirection: ((NSURLSession!, NSURLSessionTask!, NSHTTPURLResponse!, NSURLRequest!) -> (NSURLRequest!))?
-        var taskDidReceiveChallenge: ((NSURLSession!, NSURLSessionTask!, NSURLAuthenticationChallenge) -> (NSURLSessionAuthChallengeDisposition, NSURLCredential?))?
-        var taskDidSendBodyData: ((NSURLSession!, NSURLSessionTask!, Int64, Int64, Int64) -> Void)?
-        var taskNeedNewBodyStream: ((NSURLSession!, NSURLSessionTask!) -> (NSInputStream!))?
-
         init(task: NSURLSessionTask) {
             self.task = task
             self.progress = NSProgress(totalUnitCount: 0)
@@ -235,8 +232,18 @@ public class Request {
 
         // MARK: - NSURLSessionTaskDelegate
 
+        // MARK: Override Closures
+
+        var taskWillPerformHTTPRedirection: ((NSURLSession, NSURLSessionTask, NSHTTPURLResponse, NSURLRequest) -> (NSURLRequest?))?
+        var taskDidReceiveChallenge: ((NSURLSession, NSURLSessionTask, NSURLAuthenticationChallenge) -> (NSURLSessionAuthChallengeDisposition, NSURLCredential?))?
+        var taskNeedNewBodyStream: ((NSURLSession, NSURLSessionTask) -> (NSInputStream?))?
+        var taskDidCompleteWithError: ((NSURLSession, NSURLSessionTask, NSError?) -> Void)?
+
+        // MARK: Delegate Methods
+
         func URLSession(session: NSURLSession, task: NSURLSessionTask, willPerformHTTPRedirection response: NSHTTPURLResponse, newRequest request: NSURLRequest, completionHandler: ((NSURLRequest!) -> Void)) {
-            var redirectRequest = request
+            var redirectRequest: NSURLRequest? = request
+
             if taskWillPerformHTTPRedirection != nil {
                 redirectRequest = taskWillPerformHTTPRedirection!(session, task, response, request)
             }
@@ -255,6 +262,7 @@ public class Request {
                     disposition = .CancelAuthenticationChallenge
                 } else {
                     credential = self.credential ?? session.configuration.URLCredentialStorage?.defaultCredentialForProtectionSpace(challenge.protectionSpace)
+
                     if credential != nil {
                         disposition = .UseCredential
                     }
@@ -266,6 +274,7 @@ public class Request {
 
         func URLSession(session: NSURLSession, task: NSURLSessionTask, needNewBodyStream completionHandler: ((NSInputStream!) -> Void)) {
             var bodyStream: NSInputStream?
+
             if taskNeedNewBodyStream != nil {
                 bodyStream = taskNeedNewBodyStream!(session, task)
             }
@@ -274,18 +283,22 @@ public class Request {
         }
 
         func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) {
-            if error != nil {
-                self.error = error
-            }
+            if taskDidCompleteWithError != nil {
+                taskDidCompleteWithError!(session, task, error)
+            } else {
+                if error != nil {
+                    self.error = error
+                }
 
-            queue.suspended = false
+                queue.suspended = false
+            }
         }
     }
 
     // MARK: - DataTaskDelegate
 
     class DataTaskDelegate: TaskDelegate, NSURLSessionDataDelegate {
-        var dataTask: NSURLSessionDataTask! { return task as! NSURLSessionDataTask }
+        var dataTask: NSURLSessionDataTask? { return task as? NSURLSessionDataTask }
 
         private var mutableData: NSMutableData
         override var data: NSData? {
@@ -294,10 +307,7 @@ public class Request {
 
         private var expectedContentLength: Int64?
 
-        var dataTaskDidReceiveResponse: ((NSURLSession!, NSURLSessionDataTask!, NSURLResponse!) -> (NSURLSessionResponseDisposition))?
-        var dataTaskDidBecomeDownloadTask: ((NSURLSession!, NSURLSessionDataTask!) -> Void)?
-        var dataTaskDidReceiveData: ((NSURLSession!, NSURLSessionDataTask!, NSData!) -> Void)?
-        var dataTaskWillCacheResponse: ((NSURLSession!, NSURLSessionDataTask!, NSCachedURLResponse!) -> (NSCachedURLResponse))?
+
         var dataProgress: ((bytesReceived: Int64, totalBytesReceived: Int64, totalBytesExpectedToReceive: Int64) -> Void)?
 
         override init(task: NSURLSessionTask) {
@@ -307,6 +317,15 @@ public class Request {
 
         // MARK: - NSURLSessionDataDelegate
 
+        // MARK: Override Closures
+
+        var dataTaskDidReceiveResponse: ((NSURLSession, NSURLSessionDataTask, NSURLResponse) -> (NSURLSessionResponseDisposition))?
+        var dataTaskDidBecomeDownloadTask: ((NSURLSession, NSURLSessionDataTask, NSURLSessionDownloadTask) -> Void)?
+        var dataTaskDidReceiveData: ((NSURLSession, NSURLSessionDataTask, NSData) -> Void)?
+        var dataTaskWillCacheResponse: ((NSURLSession, NSURLSessionDataTask, NSCachedURLResponse) -> (NSCachedURLResponse?))?
+
+        // MARK: Delegate Methods
+
         func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveResponse response: NSURLResponse, completionHandler: ((NSURLSessionResponseDisposition) -> Void)) {
             var disposition: NSURLSessionResponseDisposition = .Allow
 
@@ -320,25 +339,27 @@ public class Request {
         }
 
         func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didBecomeDownloadTask downloadTask: NSURLSessionDownloadTask) {
-            dataTaskDidBecomeDownloadTask?(session, dataTask)
+            dataTaskDidBecomeDownloadTask?(session, dataTask, downloadTask)
         }
 
         func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveData data: NSData) {
-            dataTaskDidReceiveData?(session, dataTask, data)
-
-            mutableData.appendData(data)
+            if dataTaskDidReceiveData != nil {
+                dataTaskDidReceiveData!(session, dataTask, data)
+            } else {
+                mutableData.appendData(data)
 
-            let totalBytesReceived = Int64(mutableData.length)
-            let totalBytesExpectedToReceive = dataTask.response?.expectedContentLength ?? NSURLSessionTransferSizeUnknown
+                let totalBytesReceived = Int64(mutableData.length)
+                let totalBytesExpectedToReceive = dataTask.response?.expectedContentLength ?? NSURLSessionTransferSizeUnknown
 
-            progress.totalUnitCount = totalBytesExpectedToReceive
-            progress.completedUnitCount = totalBytesReceived
+                progress.totalUnitCount = totalBytesExpectedToReceive
+                progress.completedUnitCount = totalBytesReceived
 
-            dataProgress?(bytesReceived: Int64(data.length), totalBytesReceived: totalBytesReceived, totalBytesExpectedToReceive: totalBytesExpectedToReceive)
+                dataProgress?(bytesReceived: Int64(data.length), totalBytesReceived: totalBytesReceived, totalBytesExpectedToReceive: totalBytesExpectedToReceive)
+            }
         }
 
         func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, willCacheResponse proposedResponse: NSCachedURLResponse, completionHandler: ((NSCachedURLResponse!) -> Void)) {
-            var cachedResponse = proposedResponse
+            var cachedResponse: NSCachedURLResponse? = proposedResponse
 
             if dataTaskWillCacheResponse != nil {
                 cachedResponse = dataTaskWillCacheResponse!(session, dataTask, proposedResponse)

+ 15 - 5
Source/Upload.swift

@@ -165,16 +165,26 @@ extension Request {
     // MARK: - UploadTaskDelegate
 
     class UploadTaskDelegate: DataTaskDelegate {
-        var uploadTask: NSURLSessionUploadTask! { return task as! NSURLSessionUploadTask }
+        var uploadTask: NSURLSessionUploadTask? { return task as? NSURLSessionUploadTask }
         var uploadProgress: ((Int64, Int64, Int64) -> Void)!
 
         // MARK: - NSURLSessionTaskDelegate
 
-        func URLSession(session: NSURLSession!, task: NSURLSessionTask!, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) {
-            progress.totalUnitCount = totalBytesExpectedToSend
-            progress.completedUnitCount = totalBytesSent
+        // MARK: Override Closures
 
-            uploadProgress?(bytesSent, totalBytesSent, totalBytesExpectedToSend)
+        var taskDidSendBodyData: ((NSURLSession, NSURLSessionTask, Int64, Int64, Int64) -> Void)?
+
+        // MARK: Delegate Methods
+
+        func URLSession(session: NSURLSession, task: NSURLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) {
+            if taskDidSendBodyData != nil {
+                taskDidSendBodyData!(session, task, bytesSent, totalBytesSent, totalBytesExpectedToSend)
+            } else {
+                progress.totalUnitCount = totalBytesExpectedToSend
+                progress.completedUnitCount = totalBytesSent
+
+                uploadProgress?(bytesSent, totalBytesSent, totalBytesExpectedToSend)
+            }
         }
     }
 }