2

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];

}
swapnil
  • 43
  • 6
  • 1
    What do you mean you "background loading of [your] views"? UIKit is not thread safe (except in a very few circumstances). It's not safe to load your views on another thread. – Jason Coco Dec 29 '10 at 12:14
  • sorry the correct word would be background initialization of my views. – swapnil Dec 30 '10 at 05:22
  • Have edited the Question. Here the working thread is my secondary thread. And parseMainTextFile: is the thread launching function. This method has got a series of other methods, which initialize the child controls. Hope the scene is a clearer a bit. – swapnil Jan 03 '11 at 11:36

1 Answers1

2

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!

Community
  • 1
  • 1
Oleg Danu
  • 4,149
  • 4
  • 29
  • 47
  • Umm..Oleg thanks, but can u just post a snippet showing a scenario? Cos i didnt quite understand it well enough. – swapnil Jan 03 '11 at 05:19
  • This is how it works for me:NSRunLoop *mLoop = [NSRunLoop currentRunLoop]; self.animationTimer = [NSTimer scheduledTimerWithTimeInterval:animationInterval target:self selector:@selector(drawView) userInfo:nil repeats:YES]; mRunLoop = YES; while (![[NSThread currentThread] isCancelled] && mRunLoop && [mLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]); – Oleg Danu Jan 03 '11 at 10:15
  • umm..there still lies a confusion.. Could you please elaborate what is happening here?? – swapnil Jan 03 '11 at 11:11
  • In my case, When i rotate my parent controller, i initialize my main controller view, and to keep the UI responsive, i just fire a loader view thread and a background thread in viewDidLoad of my main Controller. As i load the basic structure from the nib, so @ rotate, the structure and the loader screen is first visible. The task of loading the child controllers of the main Controller is assigned to the background thread. Due to this while the child controllers are loading the UI stays responsive, and the User can have the autorotate feature, to rotate back to the parent if he wants. – swapnil Jan 03 '11 at 11:20
  • But this causes a problem, cos the process of loading the child controllers is lengthy, and it is divided into various different methods, cos my child controllers structures depend on the incoming data. – swapnil Jan 03 '11 at 11:24