Reputation: 1891
I am trying to adapt a tutorial about soap web requests. In the tutorial, a button click calls the sendRequest method and in didEndElement, it sets a label to the resulting "hello world". Works great. Now I want to take the sendRequest method and have it return a value. The problem is I can't seem to grasp when the invoked delegate methods are firing. This is the code I am using:
-(void) sendRequest
{
recordResults = FALSE;
NSString *soapMessage = [NSString stringWithFormat:
@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<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/\">\n"
"<soap:Body>\n"
"<HelloWorld xmlns=\"http://tempuri.org/\" />\n"
"</soap:Body>\n"
"</soap:Envelope>\n", @"test"
];
NSLog(soapMessage);
NSURL *url = [NSURL URLWithString:@"http://devportal.xxxxxxx.net/ProductCrossReference.asmx"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"http://tempuri.org/HelloWorld" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if( theConnection )
{
webData = [[NSMutableData data] retain];
}
else
{
NSLog(@"theConnection is NULL");
}
}
-(NSString*) getResult
{
return soapResults;
}
-(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
{
NSLog(@"ERROR with theConenction");
[connection release];
[webData release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"DONE. Received Bytes: %d", [webData length]);
NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
NSLog(theXML);
[theXML release];
if( xmlParser )
{
[xmlParser release];
}
xmlParser = [[NSXMLParser alloc] initWithData: webData];
[xmlParser setDelegate: self];
[xmlParser setShouldResolveExternalEntities: YES];
[xmlParser parse];
[connection release];
[webData release];
}
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qName
attributes: (NSDictionary *)attributeDict
{
if( [elementName isEqualToString:@"HelloWorldResult"])
{
if(!soapResults)
{
soapResults = [[NSMutableString alloc] init];
}
recordResults = TRUE;
}
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if( recordResults )
{
[soapResults appendString: string];
}
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if( [elementName isEqualToString:@"HelloWorldResponse"])
{
recordResults = FALSE;
[soapResults release];
}
}
Now I place this code in my view controller button click:
-(IBAction)buttonClick:(id)sender
{
SOAPService* soap = [[SOAPService alloc] init];
[soap sendRequest];
greeting.text = [soap getResult];
}
I am confused as to why getResult would fire before the invoked methods for the connection and xmlParser. As in, if I put a break point on the greeting.text = [soap getResult];
it gets hit before a break point in the -(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:
method. Shouldn't that method be called as a result of the sendRequest method? Or am I completely off base?
Upvotes: 0
Views: 114
Reputation: 7946
The connection methods are asynchronous, meaning that they do not run in series like you expect but instead run in the background, and then send a message (event) to your application once they are complete. If they did not do this then when the user pressed the button the entire interface would lock up until the SOAP request had completed.
As such, you need to add your greeting.text = [soap getResult]
to a callback (delegate method) which should be fired from
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
After the [xmlParser parse]
call.
Upvotes: 1