I am making an application that loads the contents of viewControllers using NSThread when it reads an XML file.
I did it as follows:
-(void)viewDidAppear:(BOOL)animated { // Some code... [NSThread detachNewThreadSelector:@selector(loadXML) toTarget:self withObject:nil]; [super viewDidAppear:YES]; } -(void)loadXML{ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // Read XML, create objects... [pool release]; }
My problem is that I do not know how to stop NSThread if the user switches to another viewController while NSThread is loading, which causes the application to crash.
I tried to cancel or exit NSThread as follows, but without success:
-(void)viewsDidDisappear:(BOOL)animated{ [NSThread cancel];
Can anyone help? Thanks.
source share