4

I am developing one small app in which i have multiple NSURLConnection.I have created that NSURL Connection but i don't know how to handle it.My code is like below.

-(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];
    }                  
}
Vladimir
  • 170,431
  • 36
  • 387
  • 313
Nitin
  • 7,455
  • 2
  • 32
  • 51

3 Answers3

6

Declare conn,conn1,conn2,conn3 in .h file. Then do the following. in loadTrafficAndEvent:

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

in connectionDidFinishDownloading: method,

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

}

Do the operations inside each if else condition, and no need to allocate and initialize all NSURLConnection in loadTrafficAndEvent: The download will occur one after other.

rakeshNS
  • 4,227
  • 4
  • 28
  • 42
  • :Thanks,Nice solution.But what if i want to download all data at moment with all four connection. – Nitin Jan 30 '12 at 10:49
  • Ok, first declare conn,conn1,conn2,conn3 in .h file, and in connectionDidFinishDownloading: method, check if(connection==conn){//do code here .... } else if(connection==conn1){//code ... } and so. – rakeshNS Jan 30 '12 at 10:53
1

you might want to take a look at AFNetworking for an easier and tidier way of doing network requests.

Jorge Cohen
  • 1,512
  • 10
  • 34
  • 1
    I unable run you github demo code.It show me"server did not accept client registration 68"MSG. – Nitin Jan 30 '12 at 10:52
  • I'm unfamiliar with that message but have a look over here: http://stackoverflow.com/questions/7003155/error-server-did-not-accept-client-registration-68 – Jorge Cohen Jan 30 '12 at 10:56
  • ASIHTTPRequest is the better solution for an external Framework: http://allseeing-i.com/ASIHTTPRequest/ – CarlJ Jan 30 '12 at 11:05
  • 1
    @meccan to each his own i guess, but have a look at this: http://allseeing-i.com/[request_release]; – Jorge Cohen Jan 30 '12 at 11:16
0

Implement NSURLConnection delegate methods and rest will be handle by itself

Piyush Kashyap
  • 1,965
  • 1
  • 14
  • 16