i want to know how to run any method in background...i am making app that parse xml file from internet..the file is large..so it takes 20-25 seconds to parse it and show it in table view...the problem is that when i press the button to parse the file..the button remains pressed until the file is parsed..so upto 20 sec button remains pressed..it looks odd for thr user ..(user thinks like the app hangs..)so my idea is to show the activity indicator and run the parsing method in background...when parsing ends stop the indicator and show the results in table view..any other easy way to implement the same..so that the button does not remain pressed...???
A:
Threading
Create the new thread:
[NSThread detachNewThreadSelector:@selector(myParse) toTarget:self withObject:nil];
Create the method that is called by the new thread:
- (void)myParse { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; *** code that should be run in the new thread goes here *** //sample NSURL *url; NSString* resourcePath = [[NSBundle mainBundle] resourcePath]; resourcePath = [resourcePath stringByAppendingPathComponent:@"sample.xml"]; url = [[NSURL fileURLWithPath:resourcePath] retain]; [self parsingTheXMLAtUrl:url]; [pool release]; }
see iphone sdk example for more information.
All the best.
Warrior
2010-10-15 05:54:00
+1
A:
Check out Apple's SeismicXML sample code which uses NSOperation and NSOperationQueue to download and parse XML in a background thread. That way you can see a full implementation working in the context of an actual application.
Robot K
2010-10-15 05:57:30