NSXMLParser: how do I wait until loading has finished?

Posted by Archagon on Stack Overflow See other posts from Stack Overflow or by Archagon
Published on 2011-01-04T01:50:20Z Indexed on 2011/01/04 1:53 UTC
Read the original article Hit count: 528

Filed under:
|

Let's say I'm using NSXMLParser to load a level (stored as an XML document, obviously) into my iPhone game. NSXMLParser works by assigning a delegate and sending it messages at key moments. How do I ensure that my entire level is loaded before doing anything else? I know I can make my main class the delegate and implement parserDidEndDocument, but this feels very hacky. My main class shouldn't have to know anything about the way the parsing is done! On the other hand, if I make a separate class/delegate for parsing my level, my main class has no way of knowing when the parsing is finished, unless it queries the parsing class continuously or the parsing class sends it a message. Either way, the main class would be tied to the implementation of the parsing class.

Can I hide all this event-driven business from the main class and simply make the parser return the level object when it's done? (i.e., newLevel = [[GameLevel alloc] initFromXML:xmlfile], which would in turn use NSXMLParser to load the level and then somehow return when finished.) At the moment, I'm using an external DOM parser, but I'm curious how this would be done with NSXMLParser.

Sorry if this is a stupid question -- I'm a bit new to this!

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about ios