Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASIHTTPRequest Request Cancel

I have been using ASIHTTPRequest to fetch the data and i want to cancel the request how i do it?? i do the code just like this..

-(void) serachData{
   NSURL *url= [NSURL URLWithString:self.safestring];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setTimeOutSeconds:7200];
    [request setDelegate:self];
    [request startAsynchronous];
}

 - (NSMutableDictionary *)requestFinished:(ASIHTTPRequest *)request
 {
   NSLog(@"requestFinished");
    NSString *responseString = [request responseString];
    SBJsonParser *json = [[SBJsonParser alloc] init];
    NSMutableArray *array = [[NSMutableArray alloc] initWithObjects[jsonobjectWithString:responseString], nil];
     NSLog(@"array %@",array);
  }

  - (void)requestFailed:(ASIHTTPRequest *)request{
 NSLog(@"requestFailed");
 }

//if i press cancel button(when requestFinished /requestFailed method in process ) then the ASIHTTPRequest fail and finish method Stop /abort! how i do this??

 -(IBAction)CancleREquest:(id)sender{
NSLog(@"CancleREquest");
   }
like image 712
Rinju Jain Avatar asked Dec 04 '22 16:12

Rinju Jain


2 Answers

Your cancel specific ASIHTTPRequest then :

if(![yourASIHTTPRequest isCancelled]) 
{
    // Cancels an asynchronous request
    [yourASIHTTPRequest cancel];
    // Cancels an asynchronous request, clearing all delegates and blocks first
    [yourASIHTTPRequest clearDelegatesAndCancel];
}

Note : To cancel all ASIHTTPRequest then :

for (ASIHTTPRequest *request in ASIHTTPRequest.sharedQueue.operations)
{
  if(![request isCancelled]) 
  {
     [request cancel];
     [request setDelegate:nil];
  }
}

EDIT : Use AFNetworking as ASIHTTPRequest is deprecated as its has not been update since march 2011.

like image 133
Paresh Navadiya Avatar answered Dec 26 '22 16:12

Paresh Navadiya


Nice simple version:

for (ASIHTTPRequest *request in ASIHTTPRequest.sharedQueue.operations){
    [request cancel];
    [request setDelegate:nil];
}
like image 45
frddsgn Avatar answered Dec 26 '22 15:12

frddsgn