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

我们如何在iPhone Xcode中处理多个NSURLConnection?,第1张

概述我正在开发一个小应用程序,其中我有多个NSURLConnection.I已创建NSURL连接但我不知道如何处理它.我的代码如下所示. -(void) loadTrafficAndEvent { int a=10; //Get the map view bounds for fetch the travel time markers from web service 我正在开发一个小应用程序,其中我有多个NSURLConnection.I已创建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,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,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,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];    }                  }
解决方法 在.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:下载将一个接一个地发生.

总结

以上是内存溢出为你收集整理的我们如何在iPhone Xcode中处理多个NSURLConnection?全部内容,希望文章能够帮你解决我们如何在iPhone Xcode中处理多个NSURLConnection?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1069196.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存