Cocoa:进度条仅更新到倒数第二个值,而不是最终值
我的视图上有一个带有附带标签的进度条,并且正在尝试显示文件下载的进度。这是到目前为止我的代码
-(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
-connectionDidFinishLoading:
可能会在最终-connection:didRecieveData:
消息之前发送。我会在-connectionDidFinishLoading:
上中断并检查。-connectionDidFinishLoading:
might be getting sent before the final-connection:didRecieveData:
message. I'd break on-connectionDidFinishLoading:
and check.