ASIHttp synchronous request invokes delegate methods after return

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]){
    //update content
}else{
    //launch app
}

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.

+3
source
2

, , , .

, , requestFinished "startSyncronous"? , , startSynchronous .

+1

. , ASIHttp. , , NSCondiiton. , : [self.request startSynchronous];. , signal, . :

//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];
}

... , .

0

All Articles