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。

是否有任何理由没有将最终值发送到这两个项目?

4

1 回答 1

0

-connectionDidFinishLoading:可能会在最终消息之前-connection:didRecieveData:发送。我会停下来-connectionDidFinishLoading:检查一下。

于 2010-11-18T16:47:59.207 回答