I'm an Irish iPhone developer, currently migrating my skills from the web to Apples most popular mobile computing device. Based in County Leitrim, Ireland and working from home as a freelance developer in PHP, Perl, and Objective-C.

Contact me at andy@luibh.ie.

NSXMLParser Freezing ? Put it in a thread.

Running NSXMLParser can be surprisingly time consuming when actually run on the device, even if the test data you’ve been using in the simulator gets parsed quite quickly, and takes next to no time at all.

Unfortunately when you go to run it on the iPhone itself, it can take a couple of seconds or more to do something which took milliseconds on your dev machine. This results in a fairly bad user experience when they are browsing your app, and it hangs periodically while parsing the XML.

The answer ? To move the XML parsing into a thread of its own, and let the UI continue to be useable while the parsing carries on in the background, bothering no-one else.

- (void)viewDidLoad {

[NSThread detachNewThreadSelector:@selector(parseXML) toTarget:self withObject:nil];

}

- (void) parseXML {

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

// RUN PARSING CODE HERE

[pool release];

}