I'm starting to understand RunLoop as analogous to Event Queues in Java. Wgat I'm now trying to do, merely to understand better, is create a background thread in an application that runs its own RunLoop. I get as far as this in an example ViewController and then get stuck:
@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's Docs:
Before you run a run loop on a secondary thread, you must add at least one input source or timer to it. If a run loop does not have any sources to monitor, it exits immediately when you try to run it. For examples of how to add sources to a run loop, see “Configuring Run Loop Sources.”
I'm trying to start a custom thread in my init method that will be used for arbitrary work. I'd like to send work from the "go" method into this arbitrary thread. What I don't know is how to send work into the RunLoop from something like the go method which will be connected to a Go button. Let's say I want to count from 1-10 with a small delay between each step from this secondary thread. I'd add code in my go method to schedule work using the secondary thread's RunLoop doing something like performOnThread... Do I cache a reference to this run loop on startup? How do I start the run loop and make it wait for work? (How do I configure the run loop with a custom input source?) Apparently the run method will just return if there's no timer or input source for the run loop. I've seen the documentation which discusses how to create custom sources using the CF functions but I don't see a clear example of how all this plugs together. Can somebody help?