开发者

Cocoa: progress bar only updating to penultimate value, not final

I have a progress bar with an accompanying label on my view, and am trying to display the progress of a file download. Here is my code so far

-(void)downloadXML {
    if(responseData == nil) {
        responseData = [[NSMutableData alloc] init];
    }

    progressView.progress = 0;

    NSString* updateURL = [NSString stringWithFormat:@"http://www.myserver.com/file.xml"];

    responseData = [[NSMutableData alloc] init];

    NSURLRequest* updateRequest = [NSURLRequest requestWithURL: [NSURL URLWithString:updateURL]];
    NSURLConnection* connection = [[NSURLConnection alloc] initWithRequest:updateRequest delegate:self];
    [connection start];
}


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [responseData setLength:0];
    filesize = [[NSNumber numberWithLong: [response expectedContentLength] ] retain];
}

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

    NSNumber* curLength = [NSNumber numberWithLong:[responseData length] ];
    float progress = [curLength floatValue] / [filesize floatValue] ;

    NSString *labelText = [NSString stringWithFormat:@"Downloading file: %@%", domicile, progress];

        progressView.progress = progress;
    progressLabel.text = labelText;

    NSLog(@"File Size: %f, Download Progress: %f", [filesize floatValue], progress);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
    progressView.progress = 0;
    [filesize release];
    [connection release];

}

The actual download works fine, but the progress bar and label do not seem to work quite right. The console output looks something like this which shows the file is downloading correctly.

2010-11-18 15:46:51.141 Fund Prices[8096:207] File Size: 1300.000000, Download Progress: 0.318615
2010-11-18 15:46:51.141 Fund Prices[8096:207] File Size: 1300.000000, Download Progress: 0.427615
2010-11-18 15:46:51.141 Fund Prices[8096:207] File Size: 1300.00开发者_如何转开发0000, Download Progress: 0.651215
2010-11-18 15:46:51.274 Fund Prices[8096:207] File Size: 1300.000000, Download Progress: 1.000000

However, the progress bar and the label only update to the penultimate value. i.e., the bar will progress to just over half way, and the label will update to 0.651215.

Is there any reason why the final value is not being sent to both items?


-connectionDidFinishLoading: might be getting sent before the final -connection:didRecieveData: message. I'd break on -connectionDidFinishLoading: and check.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜