Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Download progress example #13

Closed
lkraider opened this Issue Aug 23, 2011 · 9 comments

Comments

Projects
None yet
2 participants

The documentation provided shows an upload with progress reporting, but there is no similar example for how to do it when downloading data using AFNetworking.

What is the recommended way to do that, does setProgressBlock work for downloads as well as uploads? (does not seem so)

Contributor

mattt commented Aug 23, 2011

Ah, two good points there. First, progressBlock is probably not a very good name for what it does, is it? Much better if we made that explicit, like uploadProgressBlock. Second, download progress should also probably be in there, too. I had punted on that for the moment, since the expected content size from the server response has been flakey in my experience. I'll work on implementing that right away.

Contributor

mattt commented Aug 23, 2011

Alright, that was pretty easy to hook up, actually. Check out this experimental branch and let me know how it works for you!

https://github.com/gowalla/AFNetworking/tree/experimental-download-progress-block

Contributor

mattt commented Aug 23, 2011

Errata: downloadProgress currently does not work if you are sending data through an output stream.

That's great, I'm going to test it right now.

Seems to be working fine. And, as you feared, some servers don't report the content length. In my test I got for example:

Received 95893 of -1 bytes

but in my case I know the size of the file I am downloading so it is not an issue.

For the output stream, isn't it just a matter of storing the written bytes to an ivar on connection:didReceiveData: ?

@lkraider lkraider closed this Aug 23, 2011

@lkraider lkraider reopened this Aug 23, 2011

Contributor

mattt commented Aug 23, 2011

When there's an output stream, I don't keep an internal reference to the data that has been received (i.e. dataAccumulator). I was hoping that there was a more clever solution, but it seems the best workaround is to create an NSUInteger instance variable that tracks the number of bytes received so far.

Yes, I tried setting length on an empty NSMutableData instance (trying to reuse the same object), but reading the doc it actually increases the allocated size with zeroes.

What could also be interesting to have is a queue class for download/upload operations, that has multiple files combined progress reporting (actually my current use-case: fetch a JSON-encoded list and queue multiple downloads based on the returned urls).

Contributor

mattt commented Aug 24, 2011

Alright, support for download progress with an output stream is added in 8de22e8

Yeah, that queue class could be really interesting. While it's a bit niche to add to AFNetworking, I think you could implement that kind of functionality rather easily using the new download progress block. Let me know if you get anywhere with that, by the way.

Thanks again for pointing this out!

@mattt mattt closed this Aug 24, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment