下载 gzip 文件时,在 AFNetworking 3.1.0 下不会调用 downloadTaskWithRequest 中的 downloadProgress 块

downloadProgress block in downloadTaskWithRequest is not called under AFNetworking 3.1.0 When downloading a gzip file

我正在使用 AFNetworking 3.1.0 从 AWS S3 下载一些 pdf。唯一的问题是当文件被压缩时,下载进度块没有被调用。

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

NSURL *URL = [NSURL URLWithString:@"https://s3.amazonaws.com/awdtest/fullzip.pdf"];
NSURLRequest *request1 = [NSURLRequest requestWithURL:URL];

NSURLSessionDownloadTask *downloadTask1 = [manager downloadTaskWithRequest:request1 progress:^(NSProgress * _Nonnull downloadProgress)
{
    NSLog(@"Progress: %f", downloadProgress.fractionCompleted);
} destination:^NSURL *(NSURL *targetPath, NSURLResponse *response)
{
    NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
    return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error)
{
    NSLog(@"File downloaded to: %@", filePath);
}];
[downloadTask1 resume];

回复headers:

HTTP/1.1 200 OK
x-amz-id-2: CW06YcgIycOHZQy8bCJrT3aNfhatM9pty1mOjgYHumjCxRmNAQ+jhJHRQwl7mDIaQeTHI0fyrnU=
x-amz-request-id: 105E010DECC91897
Date: Sat, 14 May 2016 09:26:38 GMT
Content-Encoding: gzip
Last-Modified: Sat, 14 May 2016 09:21:29 GMT
ETag: "88bbe0b318bf11dd56a31176d3384e78"
Accept-Ranges: bytes
Content-Type: application/pdf
Content-Length: 1243325
Server: AmazonS3

如果我使用非 gzip 文件,则会调用进度块:https://s3.amazonaws.com/awdtest/full.pdf

回复headers:

HTTP/1.1 200 OK
x-amz-id-2: gFOVfhheaMdeJOBb+7H8oaXjLLeOqlQl616XnYx6C2Gj7PBVLKZ9kMIN2fJOrGBcSgQ/7nbQOc0=
x-amz-request-id: 26669D9B576E300A
Date: Sat, 14 May 2016 09:54:58 GMT
Last-Modified: Fri, 16 May 2014 05:42:35 GMT
ETag: "6f16d7e09023ce8b50fd67abba7825c4"
Accept-Ranges: bytes
Content-Type: application/pdf
Content-Length: 1411131
Server: AmazonS3

如有任何帮助,我将不胜感激。

谢谢

问题是当传输被 gzip 时,底层 NSURLSession 不知道传输的大小,因此当它调用带有更新的进度委托方法时,预期的大小是未知的。因此,完成百分比是未知的,因此很难更新 NSProgress

不过,您可以调用 setDownloadTaskDidWriteDataBlock 块,并至少在字节进入时获取字节数。您会看到 totalBytesExpectedToWrite 是负数(即 NSURLResponseUnknownLength),表明它不知道预期的字节数:

NSURLSessionDownloadTask *downloadTask1 = [manager downloadTaskWithRequest:request1 progress:^(NSProgress * _Nonnull downloadProgress) {
    NSLog(@"Progress: %f", downloadProgress.fractionCompleted);
} destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
    NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
    return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
    NSLog(@"File downloaded to: %@", filePath);
}];

[manager setDownloadTaskDidWriteDataBlock:^(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
    NSLog(@"setDownloadTaskDidWriteDataBlock: %lld %lld %lld", bytesWritten, totalBytesWritten, totalBytesExpectedToWrite);
}];

[downloadTask1 resume];

产生:

2016-05-27 15:43:21.987 MyApp[41366:3034687] setDownloadTaskDidWriteDataBlock: 25510 25510 -1
2016-05-27 15:43:22.086 MyApp[41366:3034654] setDownloadTaskDidWriteDataBlock: 18934 44444 -1
2016-05-27 15:43:22.122 MyApp[41366:3034647] setDownloadTaskDidWriteDataBlock: 19184 63628 -1
...
2016-05-27 15:43:22.884 MyApp[41366:3034698] setDownloadTaskDidWriteDataBlock: 20742 1397325 -1
2016-05-27 15:43:22.885 MyApp[41366:3034637] setDownloadTaskDidWriteDataBlock: 13806 1411131 -1
2016-05-27 15:43:23.018 MyApp[41366:3034381] File downloaded to: file:///Users/.../D8735AFD-77F4-4496-B358-162467169C96/Documents/fullzip.pdf