使用AFNetworking下载大文件 [英] Downloading large files with AFNetworking

查看:119
本文介绍了使用AFNetworking下载大文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试下载大文件并显示给用户当前进度,但阻止:

  [AFURLConnectionOperation setDownloadProgressBlock:] 

返回不正确 bytesRead totalBytesRead 值(它们比它们应该小)。



有一个90MB的文件,当它完全下载,最新块调用在 setDownloadProgressBlock:给我 totalBytesRead 值约30MB。另一方面,如果文件大小为2MB,则最新块调用会给出正确的 totalBytesRead 2MB值。



AFNetworking已更新到github的最新版本。

如果AFNetworking无法正确执行,我可以使用什么解决方案?



我确定即使文件没有完全下载(这种情况每次使用相对较大的文件时)AFNetworking调用成功阻止:

   -  [AFHTTPRequestOperation setCompletionBlockWithSuccess:failure:] 

a href =http://stackoverflow.com/questions/12722436/afimagerequestoperation-returns-image-that-not-downloaded-competely>这里关于这种情况,但没有得到任何答案。

我可以检入下载的代码和实际文件大小,但 AFNetworking 没有API可以继续部分下载。



维基解释了如何跟踪下载进度:


https://github.com/AFNetworking/AFNetworking/wiki/AFNetworking-FAQ



对于进度跟踪,不需要 bytesRead (在特定回调中写入的字节数) p>

示例代码:

  [downloadoperation setDownloadProgressBlock:^(NSUInteger bytesRead,long long totalBytesRead,long longBytesExpectedToRead){
float progress =((float)totalBytesRead)/ totalBytesExpectedToRead;
self.progressView.progress = progress;
}];


I'm trying to implement downloading of a large file and show to user current progress, but block in:

-[AFURLConnectionOperation setDownloadProgressBlock:] 

returns incorrect bytesRead and totalBytesRead values (they are smaller than they should be).

For example: If I have a 90MB file and when it downloads completely, latest block invocation in setDownloadProgressBlock: gives me totalBytesRead value about 30MB. On other side, if file is 2MB large, latest block invocation gives correct totalBytesRead 2MB value.

AFNetworking is updated to the latest version from github.
If AFNetworking can't do it correctly, what solution can I use?

Edit: I've determined that even if file is not downloaded completely (and this happens every time with relatively big file) AFNetworking calls success block in:

-[AFHTTPRequestOperation setCompletionBlockWithSuccess:failure:]

I asked a similar question here about this situation, but didn't get any answers.
I can check in code downloaded and real file sizes, but AFNetworking has no API for continuation of partial download.

解决方案

I have no problems with the setDownloadProgressBlock in AFNetworking.

The wiki explains how to track download progress: https://github.com/AFNetworking/AFNetworking/wiki/AFNetworking-FAQ

For progress tracking you don't need the bytesRead (number of bytes written in a particular callback).

Example code:

[downloadoperation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
        float progress = ((float)totalBytesRead) / totalBytesExpectedToRead;
        self.progressView.progress = progress;
    }];

这篇关于使用AFNetworking下载大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆