Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
214 views
in Technique[技术] by (71.8m points)

ios - How to integrate NSURLConnection with UIProgressView?

I'm trying to integrate a NSURLConnection object with UIProgressView, so I can update the user while a file download is happening in the background.

I created a separate object to download the file in the background, and I'm having problems figuring out how to update the progress property in the UIProgressView object with the correct value. It's probably something very simple, but I cannot figure it out with Googling around.

Here's the code that I have:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [self.resourceData setLength:0];
    self.filesize = [NSNumber numberWithLongLong:[response expectedContentLength]];
    NSLog(@"content-length: %d bytes", self.filesize);
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [self.resourceData appendData:data];

    NSNumber *resourceLength = [NSNumber numberWithUnsignedInteger:[self.resourceData length]];
    NSLog(@"resourceData length: %d", [resourceLength intValue]);
    NSLog(@"filesize: %d", self.filesize);
    NSLog(@"float filesize: %f", [self.filesize floatValue]);
    progressView.progress = [resourceLength floatValue] / [self.filesize floatValue];
    NSLog(@"progress: %f", [resourceLength floatValue] / [self.filesize floatValue]);
}

As you can see, the resourceData member variable holds the file data as it is being downloaded. The filesize member variable holds the full size of the file, as returned by my web service in its Content-Length header.

It all works sort of OK, I keep getting the downloaded data with multiple executions of didReceiveData as I should, but when I try to calculate the progress value, no proper value is returned. See below for a small sample of what I get in my console log:

content-length: 4687472 bytes
resourceData length: 2904616
filesize: 4687472
float filesize: -1.000000
progress: -2904616.000000

For reference, progressView.progress is a float. filesize is a NSNumber that holds a long long. Finally, resourceLength is a NSNumber that holds a NSUInteger.

What am I missing here?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Not sure what I'm missing here, but your filesize being -1 seems to be your problem. The API docs clearly state that expectedContentLength may not be available and that NSURLResponseUnknownLength is returned in these cases. NSURLResponseUnknownLength is defined as:

#define NSURLResponseUnknownLength ((long long)-1)

In these cases, you cannot get an accurate progress. You'll need to handle this and display an indeterminate progress meter of some sort.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...