raziiq
raziiq

Reputation: 549

Problems using NSXMLParser asynchronously

I have been developing an app for iPad and consuming the Web Service in it. Everything is working fine except that NSXMLParser is not working asynchronously.

I am getting the data from web service and feeding it to NSXMLParser like this

    xmlParser = [[NSXMLParser alloc] initWithData: webData];
   [xmlParser setDelegate: self];
   [xmlParser setShouldResolveExternalEntities:YES];
   [xmlParser parse];

I am implementing parser delegate methods didStartElement, foundCharacters and didEndElement. Here is my didEndElement method

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    NSLog(@"did end element");

    if ([elementName isEqualToString:@"authorizePassengerByEmailResult"])
    {
        NSLog(@"Inside Parser didEnd, CheckNo: %d",checkNo);

        NSLog(@"Soap Results: %@", soapResults);

        checkNo = 3;

        [soapResults setString:@""];
        elementFound = FALSE; 
    }
}

In above method checkNo is a global variable which i am using to check whether its value is changing or not in didEndElement.

In my viewController this is how i am calling the stuff

- (BOOL)textFieldShouldReturn:(UITextField *)textField {

    if (textField == txtEmail)
    {
        [txtPwd becomeFirstResponder];
    }
    if (textField == txtPwd)
    {
        [textField resignFirstResponder];
        [self.activityIndicator startAnimating];

        [self startWebServiceOperations];

        [self onLoginButtonClicked];

    }
    return YES;
}

Here are the other two mthods

- (void)onLoginButtonClicked
{

    [self.activityIndicator stopAnimating];

    if ([results isEqualToString:@"Not Authorized"])
    {

    }
    else
    {
        NSLog(@"Should be 3 but Checkno:%d",checkNo);

    }

}

- (void)startWebServiceOperations
{
    NSLog(@"I m here, Hello");

    NSString *serviceURL = @"URLAddress";

    webServices = [[WebServices alloc] init];

    NSString *strEmail = [NSString stringWithFormat:@"%@",txtEmail.text];
    NSString *strPassword = [NSString stringWithFormat:@"%@",txtPwd.text];

    [webServices createRequest:serviceURL methodNameis:@"authorizePassengerByEmail" firstParameter:strEmail secondParameter:strPassword];
}

Now the problem is i want to finish up the whole web service process and the NSXMLParser methods and then move to next function. In other words i want startWebServiceOperations function to finish completely and then move to onLoginButtonClicked function.

If i see the global variable checkNo it shows output 1 instead of 3.

Any help would be highly appreciated.

Upvotes: 0

Views: 526

Answers (1)

relower
relower

Reputation: 1313

if you call your [self onLoginButtonClicked]; method after [xmlParser parse]; like this;

xmlParser = [[NSXMLParser alloc] initWithData: webData];
   [xmlParser setDelegate: self];
   [xmlParser setShouldResolveExternalEntities:YES];
   [xmlParser parse];
[self onLoginButtonClicked];

i think this will work for you.

Upvotes: 2

Related Questions