Question

I have a main and an auxiliary thread in my app. Main thread as everyone knows is being used by UI. I use the secondary thread to do the background loading of my views. I have a main controller which i call a dummy controller. From there i call my main controller, which is in landscape mode.

So basically what i have to do is, when i rotate my dummy controller, i want to be able to load my main landscape controller and vice versa.

Everything works fine when i rotate from dummy to main. The main has a series of child controllers to load, so i have put up that process on a secondary thread, and by that time they are loading i show a loader screen.

The dummy is a normal view controller, and the main is a modal view controller.

Now the problem.

if a user is trying to rotate form dummy --> main, the rotate method kicks of the initialisation of the main view controller. But for some reason the user changes his mind and rotates back form main-->dummy ideally, the transition should be smooth as the UI thread(main theread) is free. What happens is the UI rotates back. But since the secondary thread on the Main controller is still on, even after the dismiss modal controller is called on the MainController by the main thread, when we call the willRotate function.

I've read in various post that we cannot simply kill the secondary thread, as we have to do clean up stuff ourselves. So can anyone please guide me through code, how do i kill my secondary thread and do clean up before i transit back to my calling form..?? Its kinda urgent... please dont point to other thread topics, cos i've already visited those.

I specifically want to know how should i stop my secondary thread, without causing a crash. thanks.

-(void)viewDidLoad {

[super viewDidLoad];

workingThread = [[NSThread alloc]initWithTarget:self selector:@selector(parseMainTextFile) object:nil];

[workingThread start];

[NSThread detachNewThreadSelector:@selector(addLoader) toTarget:self withObject:nil];
    //[self performSelectorOnMainThread:@selector(parseMainMatchTextFile) withObject:nil waitUntilDone:NO];

}
Was it helpful?

Solution

Generally saying, if you run a background thread it means you've got a Run Loop, which works in a while statement. You should have a flag which stops the Run Loop. this way it would transform the state of the thread to finished.

while (mRunLoop && [mLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]);

This is the way I do. Additionally read this Post and its sub posts from comments. Good luck!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top