【发布时间】:2018-10-02 23:53:56
【问题描述】:
在 Swift (Alamofire) 中,我执行以下代码:
let url = URL(string: "http://cdn.thechivemobile.com.edgesuite.net/v5/chive/20117177/20117177_2_600_366.gif")!
let task = Alamofire.request(url)
task.downloadProgress() { (progress) in
print("*** \(progress.completedUnitCount) \(progress.totalUnitCount)\n")
}
而progress.totalUnitCount 总是-1。
但是有了这个 Objective-C (AFNetworking) 代码:
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://cdn.thechivemobile.com.edgesuite.net/v5/chive/20117177/20117177_2_600_366.gif"]];
AFHTTPRequestOperation *httpOperation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[httpOperation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
NSLog(@"*** %@", @(((double)totalBytesRead) / ((double)totalBytesExpectedToRead)));
}];
[httpOperation start];
totalBytesExpectedToRead 有效。
我查看了标题,它们略有不同。
Objective-C 代码获取带有Content-Length 的标头,Swift 代码没有。
【问题讨论】:
-
你确定响应没有被缓存吗?服务器正在使用 ETag 缓存。如果响应被缓存,您将无法获得进度,因为图像实际上并未下载,而是从缓存中加载。
-
@Sulthan 我读它的方式是对象是压缩存储的。请看下面我的回答。我仍然不确定为什么 Alamofire 的做法与 AFNetworking 不同。
标签: ios swift alamofire afnetworking-3