简体   繁体   中英

Creating a custom NSRunLoop

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?

To continue, call a CFRunLoopRun$ or attach a CF or NS Timer

If you'd like to cache the run loop (for successive invocations, you may). Maybe it would be best to start by subclassing NSThread (your worker var) and providing it something to do, and something to report to when finished (this is where your real multithreaded woes begin). So the run loop attaches a timer, works until finished and sleeps if it is scheduled to continue. I hope that makes sense, as an overview.

Do I cache a reference to this run loop on startup?

That's really not necessary if you only need it from the work thread.

How do I start the run loop and make it wait for work?

You start it by starting the thread, and accessing it from the thread. Continue running by telling it to run (ie in mode) or attaching a timer.

Have you read the section on run loops in the Apple Threading Programming Guide ? That looks like a pretty good place to start. It discusses the hows and whys of run loops, as well as input sources, and the various flavors of -performSelector (one of the main ways threads communicate with one another).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM