0

I want to create two connections in a single view controller class. I am using NSOperationQueue for this purpose. The two connections are created in two functions and push inside the queue .The problem is delegates are not called. Please help me out. Thanks you in advance

- (void)viewDidLoad {
    [super viewDidLoad];
    NSOperationQueue *queue=[[NSOperationQueue alloc] init];
    NSInvocationOperation *invOperation1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(createConnection1) object:nil];
    NSInvocationOperation *invOperation2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(createConnection2) object:nil];
    NSArray *ops=[[NSArray alloc] initWithObjects:invOperation1,invOperation2,nil];
    [queue addOperations:ops waitUntilFinished:YES];

}


-(void) createConnection1{

    //create connection
    NSLog(@"Create Connection 1");
    url1 =[[NSMutableString alloc] initWithFormat:@"http://www.google.com/ig/api?weather=New Delhi"]; 

    NSURLRequest *theRequest1=[NSURLRequest requestWithURL:[NSURL URLWithString:url1]
                                               cachePolicy:NSURLRequestUseProtocolCachePolicy
                                           timeoutInterval:60.0];
    theConnection1=[[NSURLConnection alloc] initWithRequest:theRequest1 delegate:self];
    if (theConnection1) {
        connectionCreated1=YES;
        receivedData1 = [[NSMutableData data] retain];  
        NSLog(@"received data 1 %@",receivedData1);
        //[theConnection1 setDelegate:self];
    } 
}

-(void) createConnection2{
    //create connection
    NSLog(@"Create Connection 2");
    url2 =[[NSMutableString alloc] initWithFormat:@"http://www.google.com/ig/api?weather=Chennai"]; 

    NSURLRequest *theRequest2=[NSURLRequest requestWithURL:[NSURL URLWithString:url2]
                                               cachePolicy:NSURLRequestUseProtocolCachePolicy
                                           timeoutInterval:60.0];
    theConnection2=[[NSURLConnection alloc] initWithRequest:theRequest2 delegate:self];
    if (theConnection2) {
        connectionCreated2=YES;
        receivedData2 = [[NSMutableData data] retain];
        //[theConnection2 setDelegate:self];
        NSLog(@"received data 2 %@",receivedData2);
    } 

}


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    if (connectionCreated1==YES) {
        [receivedData1 setLength:0];
    }
    else if (connectionCreated2==YES) {
        [receivedData2 setLength:0];
    }
    else {
        NSLog(@"did not receive response");
    }


}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{

    if (connectionCreated1==YES) {
    [theConnection1 release];
    xmlParser1 = [[NSXMLParser alloc] initWithData:receivedData1];
        [xmlParser1 setDelegate:self];
    [xmlParser1 parse];
    }
    else if(connectionCreated2==YES){
        [theConnection2 release];
        xmlParser2 = [[NSXMLParser alloc] initWithData:receivedData2];
        [xmlParser2 setDelegate:self];
        [xmlParser2 parse];

    }



}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"connection failed" message:@"" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
    [alert show];
    [alert release];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    if (connectionCreated1==YES) {
        [receivedData1 appendData:data];
    }
    else if(connectionCreated2==YES) {
        [receivedData2 appendData:data];
    }
    else {
        NSLog(@"data not received");
    }

}
Anil Kothari
  • 7,653
  • 4
  • 20
  • 25

1 Answers1

0

The URL you have given in the first link seems to be borken ... look at this "http://www.google.com/ig/api?weather=New Delhi".. there is space between new delhi. try this instead http://www.google.com/ig/api?weather=New+Delhi

Ankit Srivastava
  • 12,347
  • 11
  • 63
  • 115