ソースを参照

Data task progress closures are now always called.

Data tasks with a registered progress closure will now always call that closure when data is received as well as update the NSProgress object stored in the `TaskDelegate`. The `Request.progress(_:)` method documentation was also updated to reflect the behavior of data tasks.

This addresses the issues reported in #407.
Christian Noon 10 年 前
コミット
a285dd541c
1 ファイル変更8 行追加4 行削除
  1. 8 4
      Source/Alamofire.swift

+ 8 - 4
Source/Alamofire.swift

@@ -647,7 +647,7 @@ public class Request {
         Sets a closure to be called periodically during the lifecycle of the request as data is written to or read from the server.
 
         - For uploads, the progress closure returns the bytes written, total bytes written, and total bytes expected to write.
-        - For downloads, the progress closure returns the bytes read, total bytes read, and total bytes expected to write.
+        - For downloads and data tasks, the progress closure returns the bytes read, total bytes read, and total bytes expected to read.
 
         :param: closure The code to be executed periodically during the lifecycle of the request.
 
@@ -871,9 +871,13 @@ public class Request {
 
             mutableData.appendData(data)
 
-            if let expectedContentLength = dataTask.response?.expectedContentLength {
-                dataProgress?(bytesReceived: Int64(data.length), totalBytesReceived: Int64(mutableData.length), totalBytesExpectedToReceive: expectedContentLength)
-            }
+            let totalBytesReceived = Int64(mutableData.length)
+            let totalBytesExpectedToReceive = dataTask.response?.expectedContentLength ?? NSURLSessionTransferSizeUnknown
+
+            progress.totalUnitCount = totalBytesExpectedToReceive
+            progress.completedUnitCount = totalBytesReceived
+
+            dataProgress?(bytesReceived: Int64(data.length), totalBytesReceived: totalBytesReceived, totalBytesExpectedToReceive: totalBytesExpectedToReceive)
         }
 
         func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, willCacheResponse proposedResponse: NSCachedURLResponse, completionHandler: ((NSCachedURLResponse!) -> Void)) {