Jens Bergvall
Jens Bergvall

Reputation: 1627

NSXMLParser and multiple calls in view's life-cycle.

Currently i'm having trouble using two functions in my -(void)viewDidLoad, both of these functions uses NSUrlRequest to send HTTPPost to a webservice to recieve data.

It works fine untill [self bar] decides to kick in before [self foo] is completely finished. So, is there any smart way of checking if [self bar] is completely finished before starting [self foo]?

-(void)viewDidLoad{

[self foo]; // initiates a nsxmlparsercall to a webservice to get values.

[self bar]; // relies on the values recieved from [self foo] to make it's own call.

            /* However, [self bar] always crashes before initiating it's request.
            /* It crashes when the variables that are being sent with the poststring 
            /* are being set, as they are null. 
            /* Which means that the `[self foo]` doesnt get completed before starting [self bar];
}

I might be very off at this point, i've even considered overriding -(void)viewDidload and setting a bool to control when it's ok to fire the second function, but that seems like super poor coding..

Any suggestions and/or tips on how to point me in the right direction will be highly appreciated. Thanks in advance.

Upvotes: 1

Views: 156

Answers (2)

Prashant Bhayani
Prashant Bhayani

Reputation: 692

you mean in [self foo] function you want to parse some thing and when its completely parsed then you want to call [self bar]; function right?

okay then you can fire a notification when parsing gets completed. in by this notification you can call the method you want.

Upvotes: 0

Robin
Robin

Reputation: 10011

I best place to put your function will be one of the delegate methods of nsxmlparser that is

- (void)parserDidEndDocument:(NSXMLParser *)parser
{
    [self bar];
}

This fine if you are parsing the response on a background thread and it doesn't matter if the function bar is called on main thread or background thread.

But if you want to call the bar function specifically on main thread then you can use this function

[self performSelectorOnMainThread:@SEL(bar) withObject:nil waitUntilDone:YES];

Upvotes: 1

Related Questions