diff --git a/Source/Alamofire.swift b/Source/Alamofire.swift index 99a1b03e8ae3aa10b003595fb778370ab66ce21f..465c57b0990091aa1223550ad9000870ec2ba062 100644 --- a/Source/Alamofire.swift +++ b/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)) {