我们如何在 iPhone Xcode 中处理多个 NSURLConnection?

Posted

技术标签:

【中文标题】我们如何在 iPhone Xcode 中处理多个 NSURLConnection?【英文标题】:How can we handle multiple NSURLConnection in iPhone Xcode? 【发布时间】:2012-01-30 10:24:39 【问题描述】:

我正在开发一个小型应用程序,其中有多个 NSURLConnection。我已经创建了该 NSURL 连接,但我不知道如何处理它。我的代码如下所示。

-(void) loadTrafficAndEvent     

    int a=10;
    //Get the map view bounds for fetch the travel time markers from web service
    MKCoordinateRegion region = mapView.region;

    float print = region.center.latitude;

   // NSLog(@"region.center=%g",print);



    CGPoint firstcorner = CGPointMake(self.mapView.bounds.origin.x , mapView.bounds.origin.y);
    CGPoint secondcorner = CGPointMake((self.mapView.bounds.origin.x+mapView.bounds.size.width) , mapView.bounds.origin.y);
    CGPoint thirdcorner  = CGPointMake(self.mapView.bounds.origin.x , (mapView.bounds.origin.y+ mapView.bounds.size.height));
    CGPoint fourthcorner = CGPointMake((self.mapView.bounds.origin.x+mapView.bounds.size.width), (mapView.bounds.origin.y + mapView.bounds.size.height));;


    //Then transform those point into lat,lng values
    CLLocationCoordinate2D mapfirstcorner,mapsecondcorner,mapthirdcorner,mapfourthcorner,requestsender;

    mapfirstcorner  = [mapView convertPoint:firstcorner toCoordinateFromView:mapView];
    mapsecondcorner = [mapView convertPoint:secondcorner toCoordinateFromView:mapView];
    mapthirdcorner  = [mapView convertPoint:thirdcorner toCoordinateFromView:mapView];
    mapfourthcorner = [mapView convertPoint:fourthcorner toCoordinateFromView:mapView];

    NSDateFormatter *dateFormatter; 
    dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MM-dd-yyyy"];    
    NSString *date = [dateFormatter stringFromDate:[NSDate date]];    
    [dateFormatter release];

    NSString *checksumString = [NSString stringWithFormat:@"TrafficAndEvents%@ForTravelStar", date];

    NSString *md5Checksum = [self getMD5CheckSum:checksumString];

    NSString *url = [NSString stringWithFormat:@"http://www.travelstar.nl/travelstarwebservice/ProviderServices.asmx/GetTrafficStatusAndEvent?northWestLatitude=%f&northWestLongitude=%f&southEastLatitude=%f&southEastLongitude=%f&zoomLevel=%d&date=%@&checksum=%@", mapfirstcorner.latitude,mapfirstcorner.longitude, self.mapView.region.center.latitude, self.mapView.region.center.longitude, a,date,md5Checksum];

    NSString *url1 = [NSString stringWithFormat:@"http://www.travelstar.nl/travelstarwebservice/ProviderServices.asmx/GetTrafficStatusAndEvent?northWestLatitude=%f&northWestLongitude=%f&southEastLatitude=%f&southEastLongitude=%f&zoomLevel=%d&date=%@&checksum=%@", mapsecondcorner.latitude,mapsecondcorner.longitude, self.mapView.region.center.latitude, self.mapView.region.center.longitude, a,date,md5Checksum];

    NSString *url2 = [NSString stringWithFormat:@"http://www.travelstar.nl/travelstarwebservice/ProviderServices.asmx/GetTrafficStatusAndEvent?northWestLatitude=%f&northWestLongitude=%f&southEastLatitude=%f&southEastLongitude=%f&zoomLevel=%d&date=%@&checksum=%@", mapthirdcorner.latitude,mapthirdcorner.longitude, self.mapView.region.center.latitude, self.mapView.region.center.longitude, a,date,md5Checksum];

    NSString *url3 = [NSString stringWithFormat:@"http://www.travelstar.nl/travelstarwebservice/ProviderServices.asmx/GetTrafficStatusAndEvent?northWestLatitude=%f&northWestLongitude=%f&southEastLatitude=%f&southEastLongitude=%f&zoomLevel=%d&date=%@&checksum=%@", mapfourthcorner.latitude,mapfourthcorner.longitude, self.mapView.region.center.latitude, self.mapView.region.center.longitude, a,date,md5Checksum];

    //Release the request if it is already created.
    if(request1 ) 
        [request release];
        request = nil;
    
    else if(request1 ) 
        [request1 release];
        request1 = nil;
    
    else if(request2 ) 
        [request2 release];
        request2 = nil;
    
    else if(request3 ) 
        [request3 release];
        request3 = nil;
    

    //Release the connection if it is already created.
    if(conn) 
        [conn cancel];
        [conn release];
        conn = nil;        
    
    else if(conn1) 
        [conn1 cancel];
        [conn1 release];
        conn1 = nil;        
    
    else if(conn2) 
        [conn2 cancel];
        [conn2 release];
        conn2 = nil;        
    
    else if(conn3) 
        [conn3 cancel];
        [conn3 release];
        conn3 = nil;        
    

    //If zoom level is grater then 6 then it will request for fetch the travel time markers from the web servce.
    if(a > 6) 

        ZOOM_LEVEL = a;
        //Create the request for fetch the data from web service.
        request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
        request1 = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:url1]];
        request2 = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:url2]];
        request3 = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:url3]];

        //NSLog(@"%@",url);
        //NSLog(@"Request sent");
        //entryDate = [NSDate date];
        //[entryDate retain];

        //Create the connection with the web service for fetch the data
     // DownloadDelegate *dd = [[DownloadDelegate alloc] init];


        conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
        conn1 = [[NSURLConnection alloc] initWithRequest:request1 delegate:self];
        conn2 = [[NSURLConnection alloc] initWithRequest:request2 delegate:self];
        conn3 = [[NSURLConnection alloc] initWithRequest:request3 delegate:self];
                      

【问题讨论】:

【参考方案1】:

在 .h 文件中声明 conn,conn1,conn2,conn3。 然后执行以下操作。 在 loadTrafficAndEvent 中:

conn1 = [[NSURLConnection alloc] initWithRequest:request1 delegate:self];

在 connectionDidFinishDownloading: 方法中,

- (void)connectionDidFinishDownloading:(NSURLConnection *)connection destinationURL:(NSURL *)destinationURL
if(connection==conn)
conn1 = [[NSURLConnection alloc] initWithRequest:request1 delegate:self];

else if(connection==conn1)
conn2 = [[NSURLConnection alloc] initWithRequest:request2 delegate:self];

else if(connection==conn2)
conn3 = [[NSURLConnection alloc] initWithRequest:request3 delegate:self];



进行每个if else条件里面的操作,不需要在loadTrafficAndEvent中分配和初始化所有的NSURLConnection:下载会一个接一个地进行。

【讨论】:

:谢谢,很好的解决方案。但是如果我想在所有四个连接的情况下下载所有数据怎么办。 好的,先在.h文件中声明conn,conn1,conn2,conn3,在connectionDidFinishDownloading:方法中,检查if(connection==conn)//这里做代码.... else if(connection==conn1)//code ... 等等。【参考方案2】:

您可能想查看AFNetworking,以更轻松、更整洁地处理网络请求。

【讨论】:

我无法运行你的 github 演示代码。它告诉我“服务器不接受客户端注册 68”MSG。 我不熟悉该消息,但请看这里:***.com/questions/7003155/… ASIHTTPRequest 是外部框架的更好解决方案:allseeing-i.com/ASIHTTPRequest @meccan 我猜是每个人自己的,但看看这个:allseeing-i.com/[request_release];【参考方案3】:

实现 NSURLConnection 委托方法,其余的将自行处理

【讨论】:

以上是关于我们如何在 iPhone Xcode 中处理多个 NSURLConnection?的主要内容,如果未能解决你的问题,请参考以下文章