使用setDidReceiveDataSelector时不保存

时间:2012-10-08 09:05:31

标签: iphone objective-c ios save asihttprequest

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

[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)];

保存已完成!

什么是问题?

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

1 个答案:

答案 0 :(得分: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]; 
}