RunLoop 的幾個run 方法

Running a Loop

1,run:


Puts the receiver into a permanent loop, during which time it processes data from all attached input sources.ide


Discussionoop

If no input sources or timers are attached to the run loop, this method exits immediately; this

otherwise, it runs the receiver in the NSDefaultRunLoopMode by repeatedly invoking runMode:beforeDate:spa

In other words, this method effectively begins an infinite loop that processes data from the run loop’s input sources and timers..net

Manually removing all known input sources and timers from the run loop is not a guarantee that the run loop will exit. OS X can install and remove additional input sources as needed to process requests targeted at the receiver’s thread. Those sources could therefore prevent the run loop from exiting.code


If you want the run loop to terminate, you shouldn't use this method. Instead, use one of the other run methods and also check other arbitrary conditions of your own, in a loop. A simple example would be:blog

BOOL shouldKeepRunning = YES;        // global
NSRunLoop *theRL = [NSRunLoop currentRunLoop];
while (shouldKeepRunning && [theRL runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]);

where shouldKeepRunning is set to NO somewhere else in the program.事件


2,- runMode:beforeDate:


Runs the loop once, blocking for input in the specified mode until a given date.ip


Return Valueci

YES if the run loop ran and processed an input source or if the specified timeout value was reached; otherwise,NO if the run loop could not be started.


Discussion

If no input sources or timers are attached to the run loop, this method exits immediately and returns NO;


 otherwise, it returns after either the first input source is processed or limitDate is reached. (每次只處理一個事件,因此runUntilDate 以及 run 要循環調用此方法, 具體緣由參考RunLoop 事件處理過程


Manually removing all known input sources and timers from the run loop does not guarantee that the run loop will exit immediately. OS X may install and remove additional input sources as needed to process requests targeted at the receiver’s thread. Those sources could therefore prevent the run loop from exiting.


NOTE

    A timer is not considered an input source and may fire multiple times while waiting for this method to return




3,- runUntilDate:


If no input sources or timers are attached to the run loop, this method exits immediately; 


otherwise, it runs the receiver in the NSDefaultRunLoopMode by repeatedly invoking runMode:beforeDate: until the specified expiration date.

相關文章
相關標籤/搜索