I am starting to understand RunLoop as a similar event queue in Java. Wgat, which I am trying to do now to understand better, creates a background thread in an application that launches its own RunLoop. I get to this in the ViewController example, and then loop around:
@implementation iPhoneRunLoopsViewController -(void) workerMain { [[NSRunLoop currentRunLoop] run]; } - (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle { worker = [[NSThread alloc] initWithTarget:self selector:@selector(workerMain) object:nil]; [worker start]; } - (IBAction)go:(id)sender { }
According to Apple Docs:
Before starting a run cycle on a secondary thread, you must add at least one input source or timer. If there are no sources to monitor in the run loop, exits immediately when you try to run it. For examples of how to add sources to the run loop, see "Configuring the Run Loop Sources".
I am trying to start my own thread in my init method, which will be used for arbitrary work. I would like to send work from the "go" method to this arbitrary stream. I don't know how to submit work to RunLoop from something like the go method, which will be connected to the Go button. Let's say I want to count from 1 to 10 with a slight delay between each step from this secondary thread. I would add code to my go method to schedule work using a secondary RunLoop thread that does something like performOnThread ... Do I cache the link to this run loop at startup? How to start a loop cycle and make it wait for work? (How to set up a run loop using a custom input source?) Obviously, the start method will simply be returned if there is no timer or input source for the start loop. I have seen documentation that discusses how to create custom sources using CF functions, but I don't see a clear example of how it all connects together. Can anyone help?
source share