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

发布于 2024-10-03 08:20:48 字数 2063 浏览 4 评论 0原文

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

-(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。

是否有任何原因导致最终值没有发送到这两个项目?

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.000000, 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

偏爱你一生 2024-10-10 08:20:48

-connectionDidFinishLoading: 可能会在最终 -connection:didRecieveData: 消息之前发送。我会在 -connectionDidFinishLoading: 上中断并检查。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文