对于烧制方法的顺序感到困惑

时间:2011-11-01 14:09:48

标签: objective-c

我正在尝试修改有关肥皂网请求的教程。在本教程中,单击按钮会调用sendRequest方法,而在didEndElement中,它会为生成的“hello world”设置标签。效果很好。现在我想采用sendRequest方法并让它返回一个值。问题是,当调用的委托方法被触发时,我似乎无法掌握。这是我正在使用的代码:

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

现在我将此代码放在我的视图控制器按钮中单击:

-(IBAction)buttonClick:(id)sender
{
    SOAPService* soap = [[SOAPService alloc] init];
    [soap sendRequest];
    greeting.text = [soap getResult];
}

我很困惑为什么getResult会在调用连接方法和xmlParser之前触发。如同,如果我在greeting.text = [soap getResult];上设置了一个断点,它会在-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:方法的断点之前被击中。不应该通过sendRequest方法调用该方法吗?还是我完全偏离基地?

1 个答案:

答案 0 :(得分:1)

连接方法是异步的,这意味着它们不会像您期望的那样以串行方式运行,而是在后台运行,然后在应用程序完成后向您的应用程序发送消息(事件)。如果他们没有这样做,那么当用户按下按钮时,整个界面将锁定,直到SOAP请求完成。

因此,您需要将greeting.text = [soap getResult]添加到应该从

触发的回调(委托方法)

-(void)connectionDidFinishLoading:(NSURLConnection *)connection

[xmlParser parse]来电之后。