Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Requests on ASIHTTPRequest

I need to download three different sets of data from three different URLs. I decided to use ASIHTTPRequest. Two of the URLs are JSON feeds which I need to parse and one of them is a .txt file online that I need to store locally.

Now the example that is on ASIHTTPRequest's website for an asynchronous request shows the following:

- (IBAction)grabURLInBackground:(id)sender {
   NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
   ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
   [request setDelegate:self];
   [request startAsynchronous];
}

To pass multiple URLs, I can call "request" on three different URLs. But I am not sure how I would handle them in the requestFinished method. The documentation shows it as:

- (void)requestFinished:(ASIHTTPRequest *)request {
   // Use when fetching text data
   NSString *responseString = [request responseString];

   // Use when fetching binary data
   NSData *responseData = [request responseData];
}

How would this method differentiate between different requests, so that I can handle it differently?

Thank you,

like image 879
darksky Avatar asked Jul 04 '11 13:07

darksky


3 Answers

You can differentiate between different requests by

  • setting the userInfo dictionary of the request
  • setting the didFinishSelector (and didFailSelector etc.) to different methods
  • using different classes as delegate
  • using blocks
  • using the request's tag property
  • subclass ASIHTTPRequest and override override requestFinished: and failWithError: (only recommended for complex situations)
like image 109
Felix Avatar answered Oct 19 '22 05:10

Felix


You just need two snippets of code. One to 'tag' your url:

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setUserInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"identifierX", @"typeOfRequest",nil]];

and another one to identify it:

if ([[[request userInfo] valueForKey:@"typeOfRequest"] isEqualToString:@"identifierX"]){
        // Do here whatever you need to do for the url associated with identifierX
    }

and that should be it!

like image 34
Eric Brotto Avatar answered Oct 19 '22 06:10

Eric Brotto


you can set the Username and tag of req.

this is the example of imageview. req.

UIImageView *imgV=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 416)];

    ASIHTTPRequest *req=[ASIHTTPRequest requestWithURL:[NSURL URLWithString:[self.arr objectAtIndex:i]]];
    [req setUsername:[NSString stringWithFormat:@"%i",i]];
    [req setUserInfo:[NSDictionary dictionaryWithObjectsAndKeys:imgV,@"imgV",nil]];
    [req setDelegate:self];
    [req startAsynchronous];
    [imgV setContentMode:UIViewContentModeScaleToFill];
    [imgV setClipsToBounds:YES];
    [imgV setTag:kTagImageViewInScrollView];
    [scr2 addSubview:imgV];
    [scr2 setDelegate:self];
    [imgV release]; imgV=nil;

and in requestFinished

- (void)requestFinished:(ASIHTTPRequest *)request {
    [(UIImageView*)[[request userInfo] valueForKey:@"imgV"] setImage:[UIImage imageWithData:[request responseData]]];   

}
like image 1
Rakesh Bhatt Avatar answered Oct 19 '22 06:10

Rakesh Bhatt