在Objective-C中执行一系列SOAP请求的更好方法

我使用以下hack-job代码来执行一系列从服务器下载数据以供在应用程序中使用的SOAP请求:

这个代码在按下“更新”button时被调用:

- (IBAction) update { UIAlertView *errorView; if([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] == NotReachable) { errorView = [[UIAlertView alloc] initWithTitle: @"Network Error" message: @"No Network connection availible!" delegate: self cancelButtonTitle: @"OK" otherButtonTitles: nil]; [errorView show]; } else { [appDelegate.categories removeAllObjects]; [appDelegate.currencies removeAllObjects]; [appDelegate.projects removeAllObjects]; HUD = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES]; HUD.labelText = @"Downloading.."; [self requestCategories]; } } 

下面是一个典型的要求,我使用了大约6个。

 // SOAP requests - (void) requestCategories { // Indeterminate mode categories = [[NSMutableArray alloc] init]; xmlBlock = CATEGORY; NSString *soapMsg = [NSString stringWithFormat: @"<?xml version=\"1.0\" encoding=\"utf-8\"?> <soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"> <soap:Body> <Categories xmlns=\"http://tempuri.org/\"> <UID>string</UID> <Username>string</Username> <Password>string</Password> </Categories> </soap:Body> </soap:Envelope>" ]; //---print it to the Debugger Console for verification--- NSLog(@"%@", soapMsg); NSURL *url = [NSURL URLWithString: @"http://www.$$%$%^^^%$$££.co.uk/%$^£^£^$&£.asmx"]; NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url]; //---set the headers--- NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]]; [req addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; [req addValue:@"http://tempuri.org/Categories" forHTTPHeaderField:@"SOAPAction"]; [req addValue:msgLength forHTTPHeaderField:@"Content-Length"]; //---set the HTTP method and body--- [req setHTTPMethod:@"POST"]; [req setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]]; //[activityIndicator startAnimating]; conn = [[NSURLConnection alloc] initWithRequest:req delegate:self]; if (conn) { webData = [NSMutableData data]; } } 

以下是NSURLConnection的委托方法(和parsing方法):

 -(void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *) response { [webData setLength: 0]; } -(void) connection:(NSURLConnection *) connection didReceiveData:(NSData *) data { [webData appendData:data]; } -(void) connection:(NSURLConnection *) connection didFailWithError:(NSError *) error { } -(void) connectionDidFinishLoading:(NSURLConnection *) connection { NSLog(@"DONE. Received Bytes: %d", [webData length]); NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding]; //---shows the XML--- NSLog(@"%@", theXML); if (xmlBlock == CATEGORY){ [self parseXML:webData]; [self requestCurrencies]; } else if (xmlBlock == CURRENCY){ [self parseXML:webData]; [self requestNominals]; } else if (xmlBlock == NOMINAL){ [self parseXML:webData]; [self requestProjects]; } else if (xmlBlock == PROJECT){ [self parseXML:webData]; [self requestRegister]; } else { [self parseXML:webData]; HUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]]; HUD.labelText = @"Done!"; HUD.mode = MBProgressHUDModeCustomView; [HUD hide:YES afterDelay:2]; } } - (void) parseXML: (NSMutableData *)localWebData { xmlParser = [[NSXMLParser alloc] initWithData: localWebData]; [xmlParser setDelegate: self]; [xmlParser setShouldResolveExternalEntities:YES]; [xmlParser parse]; } 

我不认为你需要看到我的XMLparsing委托方法(如果你让我知道)。 我的问题是,有没有更好的方式来实现我的应用程序中的这个function? 如同在向用户显示某种进度指示器的过程中一个接一个地执行请求吗?

谢谢,

插口

使用NSOperation队列,即使你的类成为NSOperation的一个子类,在这个子类中你发送请求到服务,并且把你的方法重命名为main。 然后在父类中创build该类的属性,并将所有的请求添加到操作队列中。 而为了完成,使用keyobserver你nsopertion子类的属性