1

我想下载一个文件并显示
我能够做到这一点的进度条。
现在,我想在标签中显示进度值并使用此代码来进行初始化和更新标签:

[queue setDelegate:self];
[queue setRequestDidFinishSelector:@selector(updateLabel)];
[queue setDownloadProgressDelegate:progress];
[queue setShowAccurateProgress:YES];

ASIHTTPRequest *request;
request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];

[request setTemporaryFileDownloadPath:[filePath stringByAppendingString:@".download"]];
[request setAllowResumeForFileDownloads:YES];

[request setDidFinishSelector:@selector(updateLabel)];
[request setDidReceiveDataSelector:@selector(updateLabel)];

[request setShouldContinueWhenAppEntersBackground:YES];
[request setShouldAttemptPersistentConnection:NO]; 
[request setDownloadDestinationPath:filePath]; 
[queue addOperation:request];
[queue go];

但不保存在目标路径中!

当我清除此代码时: 

[request setDidReceiveDataSelector:@selector(updateLabel)];

保存完成!

什么是问题?

我想在进度值更改时更新标签文本

4

1 回答 1

0

这是您需要对主线程执行的操作。更新应用程序的 UI 由主线程而不是任何后台线程执行。

或者

或者,您可以使用以下适用于我的代码片段:

- (void)fetchThisURLFiveTimes:(NSURL *)url
{
   ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
   [request addOperation:request];
   [request cancelAllOperations];
   [request setDownloadProgressDelegate:myProgressIndicator];
   [request setDelegate:self];
   [request setRequestDidFinishSelector:@selector(queueComplete:)];

   [request go];
}

- (void)queueComplete:(ASINetworkQueue *)queue
{
   NSLog(@"Value: %f", [myProgressIndicator progress]);
   [self performSelectorOnMainThread:@selector(updateLabel) withObject:nil waitUntilDone:NO]; 
}
于 2012-10-08T09:35:52.693 回答