Multiple Requests on ASIHTTPRequest

泄露秘密 提交于 2019-11-30 14:21:20

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)

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!

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

}

You can check the originalURL property of ASIHTTPRequest if you have different URLS.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!