ビューにラベルが付いた進行状況バーがあり、ファイルのダウンロードの進行状況を表示しようとしています。これがこれまでの私のコードです
-(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
ただし、プログレスバーとラベルは最後から2番目の値にのみ更新されます。つまり、バーは途中まで進み、ラベルは0.651215に更新されます。
最終的な値が両方のアイテムに送信されない理由はありますか?