If you run the run loop in a non-main thread, you must add at least one input sources or timer for the run loop. If the run loop you run does not monitor any input sources, the run loop will exit immediately after you run it.
Run loop observer
Use detachnewthreadselector: totarget: withobject: to create a thread:
[NSThread detachNewThreadSelector:@selector(observerRunLoop) toTarget:self withObject:nil];
Add observer to the run loop of the new thread:
-(Void) observerrunloop {// create an automatic release pool NSAID utoreleasepool * Pool = [[NSAID utoreleasepool alloc] init]; // obtain the current thread's run loop nsunloop * myrunloop = [nsunloop currentrunloop]; // set the runtime environment of the run loop observer cfrunloopobservercontext context = {0, self, null, null, null}; // create the run loop observer object // The first parameter is used to allocate the memory of the observer object // The second parameter is used to set the event to be followed by the observer, for more information, see the callback function myrunloopobserver. // The third parameter is used to identify the observer. It is executed when you first enter the run loop. Or is the fourth parameter executed every time you enter the run loop processing? // The fourth parameter is used to set the priority of the observer. // The Fifth parameter is used to set the callback function of the observer. // The sixth parameter is used to set observer runtime environment cfrunloopobserverref observer = cfrunloopobservercreate (kcfallocatordefault, dependencies, yes, 0, & myrunloopobserver, & context); If (observer) {// converts the cocoa's nsunloop type to the cfrunloopref type cfrunloopref cfrunloop = [myrunloop getcfrunloop]; // Add the newly created observer to the run loop cfrun of the current thread Loopaddobserver (cfrunloop, observer, kcfrunloopdefadefamode);} // creates and returns a new nstimer object and schedules it on the current run loop in the default mode [nstimer progress: 0.1 target: self selector: @ selector (dofiretimer :) userinfo: Nil repeats: Yes]; nsinteger loopcount = 10; do {// start the loop of the current thread until the specified time arrives, when a loop is running, run loop processes all input source data from the run loop. // For example, the input source associated with the current run loop has only one timer type source. // The timer sends a trigger event to the run loop every 0.1 seconds. When the run loop detects the event, it calls the corresponding processing method. // Because the observer is added to the run loop and the observer is set to be interested in all the run loop behaviors. // When the rununitdate method is called, The Observer detects that the run loop starts and enters the loop. The observer calls its callback function, and the second parameter passes the kcfrunloopentry action. // The operations that the observer detects other run loop behaviors and calls the callback function are similar to those described above. [Myrunloop rununtildate: [nsdate datewithtimeintervalsincenow: 1.0]; // when the running time of the run loop is reached, the current run loop is exited. The observer also detects the exit behavior of the run loop and calls its callback function. The behavior passed by the second parameter is kcfrunloopexit. Loopcount --;} while (loopcount); // release the automatically released pool [pool release];}
Set the observer callback function:
void myRunLoopObserver(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void *info) { switch (activity) { //The entrance of the run loop, before entering the event processing loop. //This activity occurs once for each call to CFRunLoopRun and CFRunLoopRunInMode case kCFRunLoopEntry: NSLog(@"run loop entry"); break; //Inside the event processing loop before any timers are processed case kCFRunLoopBeforeTimers: NSLog(@"run loop before timers"); break; //Inside the event processing loop before any sources are processed case kCFRunLoopBeforeSources: NSLog(@"run loop before sources"); break; //Inside the event processing loop before the run loop sleeps, waiting for a source or timer to fire. //This activity does not occur if CFRunLoopRunInMode is called with a timeout of 0 seconds. //It also does not occur in a particular iteration of the event processing loop if a version 0 source fires case kCFRunLoopBeforeWaiting: NSLog(@"run loop before waiting"); break; //Inside the event processing loop after the run loop wakes up, but before processing the event that woke it up. //This activity occurs only if the run loop did in fact go to sleep during the current loop case kCFRunLoopAfterWaiting: NSLog(@"run loop after waiting"); break; //The exit of the run loop, after exiting the event processing loop. //This activity occurs once for each call to CFRunLoopRun and CFRunLoopRunInMode case kCFRunLoopExit: NSLog(@"run loop exit"); break; /* A combination of all the preceding stages case kCFRunLoopAllActivities: break; */ default: break; }}
Start run Loop
To start the run loop, set a time limit to enable it in special mode.
Entering the run loop in unconditional mode is the simplest choice, but it is not the best choice. Running the run loop in an unconditional way will bring the thread into a permanent loop. Such an operation will make it difficult for you to control the run loop. You can add the input source or timer for the run loop, but the method to exit the run loop is kill. In this case, the run loop cannot run in custom mode.
Unlike running the run loop in unconditional mode, it is better to start the run loop in time-limited mode. When you use the timeout time to limit the running of the run loop, the run loop runs until the event arrives or the timeout time is reached. If the event arrives, run loop distributes the event to the handler (processor) for processing and exits after the processing is complete. Your code then restarts the run loop to process the next event. If you quit because the timeout time is reached, you can simply restart
The run loop or use the time to do any needed housekeeping.
In addition to the timeout time, you can also run the run loop in a specific mode. The mode and timeout are not mutually exclusive. You can specify the timeout time and mode when starting a run loop. The Mode limits the type of sources that a shipping event sends to the run loop. For details, see "iPhone -- nsunloop concept ".
Exit run Loop
To exit the run loop, configure the run loop startup time-out and explicitly stop the run loop (call the cfrunloopstop function ).