Objective c ASIHttp同步请求在返回后正在运行委托方法
我正在尝试从服务器下载文件。我的代码如下。在didFinishLaunchingWithOptions方法中,我使用运行以下代码的detachNewThreadSelector创建了一个新线程Objective c ASIHttp同步请求在返回后正在运行委托方法,objective-c,download,asihttprequest,Objective C,Download,Asihttprequest,我正在尝试从服务器下载文件。我的代码如下。在didFinishLaunchingWithOptions方法中,我使用运行以下代码的detachNewThreadSelector创建了一个新线程 NSString *destPath = [self.home_dir_path stringByAppendingPathComponent:[NSString stringWithFormat:@"_%@",content_data_file_name]]; [ContentBO downloadFi
NSString *destPath = [self.home_dir_path stringByAppendingPathComponent:[NSString stringWithFormat:@"_%@",content_data_file_name]];
[ContentBO downloadFile:destPath content_name:content_data_file_name];
if([self updatesAvailable]){
//update content
}else{
//launch app
}
我下载文件的代码是:
@try{
NSString *url = [NSString stringWithFormat:@"%@/%@",ServerURL,content_name];
NSLog(@"downloading URL is: %@",url);
self.request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:[url stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]]];
[self.request setRequestMethod:@"GET"];
[self.request setDownloadDestinationPath:destFilePath];
NSLog(@"destination path is: %@",destFilePath);
[self.request setTimeOutSeconds:30];
[self.request setDelegate:self];
[self.request startSynchronous];
NSError *error = [self.request error];
NSData *receivedData = nil;
if (!error) {
isSuccess = YES;
self.responseStr = [request responseString];
receivedData = [NSData dataWithData:[self.request responseData]];
}
else {
isSuccess = NO;
NSLog(@"The following error occurred: %@", error);
}
}@catch(NSException *e){
NSLog(@"exception occured.");
}
我对同步调用的理解是,这是一个阻塞调用,控制不应该放在下面
[ContentBO downloadFile:destPath content_name:content_data_file_name];
直到控件脱离请求,ASIHTTPRequestDelegate的完成方法。在我的例子中,发生的是控件同时执行requestFinished和below中的代码
[ContentBO downloadFile:destPath content_name:content_data_file_name];
但是我不希望控件在退出requestFinished方法之前低于[ContentBO downloadFile…。requestFinished委托调用在主线程上异步运行,并且您的代码没有在主线程上运行,因此预计两者都将同时运行
但是,在使用同步请求时,为什么不删除requestFinished的内容并将代码放在“startSyncronous”行之后?当startSynchronous返回时,您可以保证请求已完成。在我的一个项目中,应用程序必须进行大量服务器端数据同步。在这个过程中,一个操作应该在成功执行它的前一个过程之后启动,我在这个过程中使用了ASIHttp同步请求。我面临着你提到的同一个问题,所以为了解决这个问题,我使用了NSCondiiton。调用以下命令后,需要锁定线程: [自请求启动同步];。在执行请求后调用requests delegate方法时,发出一个signal命令,将执行thread lock语句后的下一行代码。下面是一个粗略的例子:
//declare a pointer to NSCondition in header file:
NSCondition *threadlock;
-(id) init
{
threadlock = [[NSCondition alloc] init]; //release it in dealloc
}
-(void)downLoadFile
{
[thread lock];
//your request code
[self.request setDidFinishSelector:@selector(downLoadFileRequestDone:)];
[self.request setDidFailSelector:@selector(downLoadFileRequestWentWrong:)];
[self.request startSynchronous];
[thread wait];
//the lines here will be executed after you issue a signal command in the request delegate method
[thread unlock];
}
-(void) downLoadFileRequestDone:(ASIHTTPRequest *)request
{
[thread lock];
//perform desire functionality
//when you are done call:
[thread signal];
[thread unlock];
}
这对我来说非常好。。。希望这会有帮助