Tutorial :Is the ASIHTTPRequest is not asynchronous?



Question:

I am using ASIHTTPRequest to fetch some data from a web service.

I am making requests using a loop.

The problem is that it doesn't seem the request is going asynchronously so that my activityindicator is not working .

Is it true that ASIHTTPRequest is not asynchronous .

or should i use the regular nsmutablerequest to perform asynchronous request .


Solution:1

You should put your request in a download queue, i.e.

ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:url];  [request setDelegate:self];  [request setDidFinishSelector:@selector(requestDone:)];  [request setDidFailSelector:@selector(requestWentWrong:)];  NSOperationQueue *queue = [[NSOperationQueue alloc] init];  [queue addOperation:request];  [request release];      

Just

[request startAsynchronous];   

runs the request on the UI thread, so you should try it with download queue.


Solution:2

For Sync Synchronous

  NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];    [request startSynchronous];    NSError *error = [request error];    if (!error) {      NSString *response = [request responseString];    }  

Creating an asynchronous request

- (IBAction)grabURLInBackground:(id)sender  {     NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];     ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];     [request setDelegate:self];     [request startAsynchronous];  }    - (void)requestFinished:(ASIHTTPRequest *)request  {     // Use when fetching text data     NSString *responseString = [request responseString];       // Use when fetching binary data     NSData *responseData = [request responseData];  }    - (void)requestFailed:(ASIHTTPRequest *)request  {     NSError *error = [request error];  }  

And there is lot of more options like Queue and many more

You can refer http://allseeing-i.com/ASIHTTPRequest/How-to-use


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »