0

我想在单个视图控制器类中创建两个连接。为此我使用NSOperationQueue。两个连接创建在两个函数中,并推入队列中。问题是代理不被调用。请帮助我。感谢您提前异步连接没有被调用

- (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"); 
    } 

} 
+0

请请帮帮我。我很麻烦 –

回答

0

你在第一个链接中给出的网址似乎是博肯......看看这个“http://www.google.com/ig/api?weather=New Delhi” ..还有就是新德里之间的空间。试试这个代替http://www.google.com/ig/api?weather=New+Delhi

+0

这两个链接都在浏览器中工作,但没有人返回xcode中的数据。即没有一个代表被称为。 –