Download large files using AFNetworking

I am trying to implement downloading a large file and show the current move of the user, but block it:

-[AFURLConnectionOperation setDownloadProgressBlock:] 

returns invalid bytesRead and totalBytesRead (they are less than they should be).

For example: if I have a file of 90 MB, and when it is fully loaded, the last call to the block in setDownloadProgressBlock: gives me a totalBytesRead value of about 30 MB. On the other hand, if the file is 2 MB in size, the last call to the block gives the correct 2 MB totalBytesRead .

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

Edit: I determined that even if the file is not downloaded completely (and this happens each time with a relatively large file) AFNetworking causes a success block in:

 -[AFHTTPRequestOperation setCompletionBlockWithSuccess:failure:] 

I asked a similar question here about this situation, but received no answers.
I can check the downloaded code and the actual file sizes, but AFNetworking does not have an API to continue partial downloads.

+4
source share
2 answers

I have no problem with setDownloadProgressBlock in AFNetworking.

Wikis explain how to track download progress: https://github.com/AFNetworking/AFNetworking/wiki/AFNetworking-FAQ

You don't need bytesRead to track progress (the number of bytes written in a particular callback).

Code example:

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

phix23 answered my initial question, so there really is no problem with the progress block. I am responding to my editing.

When the success block in

 -[AFHTTPRequestOperation setCompletionBlockWithSuccess:failure:] 
Called

I can compare the number of bytes loaded with the number of expected bytes (this is not very difficult to implement in the code). If the number of bytes downloaded is less, I can continue to download the file from the place where it was interrupted. For this, I use the Range http header in NSURLRequest , here is some additional information:
Continue Aborted Download on iPhone
How to implement renewable file downloads on the iPhone SDK

Thus, the problem is not that AFNetworking (NSURLConnection has the same behavior), but, I think, structure developers can take this situation into account.

+1
source

Source: https://habr.com/ru/post/1443794/


All Articles