I suspect you'll find the problem is totalBytesExpectedToRead
. Specifically, this comes from the NSURLResponse
property, expectedContentLength
, which as the docs say:
Return Value
The receiver’s expected content length, or
NSURLResponseUnknownLength
if the length can’t be determined.Discussion
Some protocol implementations report the content length as part of the response, but not all protocols guarantee to deliver that amount of data. Clients should be prepared to deal with more or less data.
By the way, this constant, NSURLResponseUnknownLength
, is equal to -1, which would explain your calculated values. This is just the way the system informs you that it was unable to determine the length of the response.
So, in the worst case scenario, you'll be getting -1 as a number of bytes expected. Even if it's not -1, it is not entirely reliable (especially an issue if you have written your own server code). Your app must gracefully handle whatever expectedContentLength
is reported to your app. Most of the time, if you're getting a non-negative value, you're generally getting a value that can be used effectively for a "percent complete" calculation, but don't rely upon it (e.g. you may receive more bytes or less bytes than expectedContentLength
reports). Gracefully handle exceptional values.
For example, when setting my progress bar, I've done something like:
CGFloat progress;
if (expectedContentLength > 0 && progressContentLength <= expectedContentLength)
progress = (CGFloat) progressContentLength / expectedContentLength;
else
progress = (progressContentLength % 1000000l) / 1000000.0f;
This gives me a progress bar that progresses slowly to 100% as the download proceeds if I've got good expected content length value, but otherwise shows me a progress bar that progresses from 0% to 100% for every 1,000,000 bytes downloaded. This latter scenario is not ideal, but there's not a lot you can do unless your server reports an accurate expected content length.
Alternatively, if you never get a reliable expectedContentLength
, you might choose to use an indeterminate progress view (e.g. UIActivityIndicatorView
) which avoids this problem altogether.
Bottom line, the successful calculation of progress is contingent upon your server is providing a legitimate value for bytes expected. If you look at totalBytesExpectedToRead
, I'd bet you're getting -1, and seeing your curious calculated progress percentage values as a result.