Search code examples
iphoneobjective-cjsonasihttprequest

Network Queue is not responding to second request


I am creating a network queue to send two requests.I have added these two request in network queue.

-(void)viewDidLoad 
{
    [super viewDidLoad];
    ASINetworkQueue *networkQueue = [[ASINetworkQueue queue] retain];
    NSURL *url = [NSURL URLWithString:@"http://www.w3schools.com/xml/simple.xml"];
    NSURL *url1 = [NSURL URLWithString:@"http://search.twitter.com/search.json?q=mobtuts&rpp"];

    for(int i = 0; i<=1; i++)
    {
        if(i==0)
        {
            ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
            NSString *str =[NSString stringWithFormat: @"%d", i];
            request.userInfo = [NSDictionary dictionaryWithObject:str forKey:@"requestnum"];
            [request setDelegate:self];
            [networkQueue addOperation:request];
        }
        if(i==1)
        {
            ASIHTTPRequest *request1 = [ASIHTTPRequest requestWithURL:url1];
            NSString *str =[NSString stringWithFormat: @"%d", i];
            request1.userInfo = [NSDictionary dictionaryWithObject:str forKey:@"requestnum1"];
            [request1 setDelegate:self];
            [networkQueue addOperation:request1];   
        }
        [networkQueue go];
    }
}

For checking my responce i am getting the responce in requestFinished:(ASIHTTPRequest *)request methode, it always shows the first xml data as responce(NSLog(@"%@",responseString);), if i change my key from requestnum to requestnum1 i get the first xml data as responceString,why i am not able to access the second request's responce.

-(void)requestFinished:(ASIHTTPRequest *)request
{
    NSString *str ;
    str= [request.userInfo objectForKey:@"requestnum1"];
    NSString *responseString = [request responseString];
    str = [request responseString];
    NSLog(@"%@",responseString);
}

Solution

  • Its very simple to differentiate both request. Just assign tag to each request.

    for(int i = 0; i<=1; i++)
    {
        if(i==0)
        {
            ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
            NSString *str =[NSString stringWithFormat: @"%d", i];
            request.tag = 1; //<--Tag here
            request.userInfo = [NSDictionary dictionaryWithObject:str forKey:@"requestnum"];
            [request setDelegate:self];
            [networkQueue addOperation:request];
        }
        if(i==1)
        {
            ASIHTTPRequest *request1 = [ASIHTTPRequest requestWithURL:url1];
            NSString *str =[NSString stringWithFormat: @"%d", i];
            request1.tag = 2; //<--Tag here
            request1.userInfo = [NSDictionary dictionaryWithObject:str forKey:@"requestnum1"];
            [request1 setDelegate:self];
            [networkQueue addOperation:request1];   
        }
        [networkQueue go];
    }
    

    In request finish handle accordingly

    -(void)requestFinished:(ASIHTTPRequest *)request
    {
        NSLog(@"request tag-->%d",request.tag);
    
        NSString *str=nil;
        NSString *responseString = nil;
    
        switch (request.tag) 
        {
            case 1:
    
                str= [request.userInfo objectForKey:@"requestnum"];
                responseString = [request responseString];
                str = [request responseString];
                NSLog(@"Response reqeust 1 --->%@",responseString);
    
                break;
    
            case 2:
                str= [request.userInfo objectForKey:@"requestnum1"];
                responseString = [request responseString];
                NSLog(@"Response reqeust 2 --->%@",responseString);
    
                break;
            default:
                break;
        }
    }
    

    Enjoy.