I am trying to download a file from the server. My code is as follows. In the didFinishLaunchingWithOptions method, I create a new thread using detachNewThreadSelector, which runs the following code.
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]){
}else{
}
My code to download File:
@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.");
}
What my understanding of a synchronous call is that it is a blocking call, and control should not go below
[ContentBO downloadFile:destPath content_name:content_data_file_name]
until control is complete. Fixed ASIHTTPRequestDelegate method. In my case, what happens is that the control simultaneously executes the code in requestFinished and below
[ContentBO downloadFile:destPath content_name:content_data_file_name]
But I do not want the control to be below [ContentBO downloadFile ...] before exiting the requestFinished method.
source