可可:进度条只更新到倒数第二个值,而不是最终值

时间:2010-11-18 16:00:57

标签: objective-c progress-bar progress

我的视图中有一个附带标签的进度条,我正在尝试显示文件下载的进度。这是我到目前为止的代码

-(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];

}

实际下载工作正常,但进度条和标签似乎不能正常工作。控制台输出看起来像这样,显示文件正在正确下载。

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.000000, Download Progress: 0.651215
2010-11-18 15:46:51.274 Fund Prices[8096:207] File Size: 1300.000000, Download Progress: 1.000000

但是,进度条和标签仅更新为倒数第二个值。即,条形图将进展到一半以上,标签将更新为0.651215。

有没有理由不将最终值发送到这两个项目?

1 个答案:

答案 0 :(得分:0)

可能会在最终-connectionDidFinishLoading:消息之前发送

-connection:didRecieveData:。我打破了-connectionDidFinishLoading:并检查。