Multiple ASIHTTPRequest Requests

I need to load three different datasets from three different URLs. I decided to use ASIHTTPRequest. Two of the URLs are the JSON channels I need to parse, and one of them is a .txt file that I need to store locally.

Now the example that is on the ASIHTTPRequest 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 a β€œrequest” at three different URLs. But I'm not sure how I will handle them in the requestFinished method. The documentation shows this 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 distinguish between different requests so that I could handle it differently?

Thanks,

+6
source share
4 answers

You can distinguish between different requests

  • setting dictionary userInfo request
  • setting didFinishSelector (and didFailSelector etc.) to various methods.
  • using different classes as a delegate
  • using blocks
  • using request tag property
  • subclass ASIHTTPRequest and override the override request Finished: and failWithError: (recommended only for complex situations)
+12
source

You just need two pieces of code. One to "tag" your URL:

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

and one more to identify it:

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

and it should be!

+3
source

you can set username and req tag.

This is an example image. 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]]]; } 
+1
source

You can check the originalURL property for ASIHTTPRequest if you have different URLs.

Or you can use the [hash of the request] to get an NSObject hash for each object and check it later.

+1
source

Source: https://habr.com/ru/post/891962/


All Articles