Inherits from | |
Conforms to | |
Framework | /System/Library/Frameworks/Foundation.framework |
Availability | Available in Mac OS X v10.0 and later. |
Companion guide | |
Declared in | NSRunLoop.h |
Related sample code |
The NSRunLoop
class declares the programmatic interface to objects that manage input sources. An NSRunLoop
object processes input for sources such as mouse and keyboard events from the window system, NSPort
objects, and NSConnection
objects. An NSRunLoop
object also processes NSTimer
events.
In general, your application does not need to either create or explicitly manage NSRunLoop
objects. Each NSThread
object, including the application’s main thread, has an NSRunLoop
object automatically created for it as needed. If you need to access the current thread’s run loop, you do so with the class method currentRunLoop
.
Note that from the perspective of NSRunloop
, NSTimer
objects are not "input"—they are a special type, and one of the things that means is that they do not cause the run loop to return when they fire.
Warning:
The NSRunLoop
class is generally not considered to be thread-safe and its methods should only be called within the context of the current thread. You should never try to call the methods of an NSRunLoop
object running in a different thread, as doing so might cause unexpected results.
– configureAsServer
Deprecated in Mac OS X v10.5
– performSelector:target:argument:order:modes:
– cancelPerformSelector:target:argument:
– cancelPerformSelectorsWithTarget:
Returns the NSRunLoop
object for the current thread.
+ (NSRunLoop *)currentRunLoop
The NSRunLoop
object for the current thread.
If a run loop does not yet exist for the thread, one is created and returned.
NSRunLoop.h
Returns the run loop of the main thread.
+ (NSRunLoop *)mainRunLoop
An object representing the main thread’s run loop.
NSRunLoop.h
Runs the loop once or until the specified date, accepting input only for the specified mode.
- (void)acceptInputForMode:(NSString *)mode beforeDate:(NSDate *)limitDate
The mode in which to run. You may specify custom modes or use one of the modes listed in “Run Loop Modes.”
The date up until which to run.
If no input sources or timers are attached to the run loop, this method exits immediately; otherwise, it runs the run loop once, returning as soon as one input source processes a message or the specifed time elapses.
Note: A timer is not considered an input source and may fire multiple times while waiting for this method to return
Manually removing all known input sources and timers from the run loop is not a guarantee that the run loop will exit. Mac 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.
NSRunLoop.h
Adds a port as an input source to the specified mode of the run loop.
- (void)addPort:(NSPort *)aPort forMode:(NSString *)mode
The port to add to the receiver.
The mode in which to add aPort. You may specify a custom mode or use one of the modes listed in “Run Loop Modes.”
This method schedules the port with the receiver. You can add a port to multiple input modes. When the receiver is running in the specified mode, it dispatches messages destined for that port to the port’s designated handler routine.
NSRunLoop.h
Registers a given timer with a given input mode.
- (void)addTimer:(NSTimer *)aTimer forMode:(NSString *)mode
The timer to register with the receiver.
The mode in which to add aTimer. You may specify a custom mode or use one of the modes listed in “Run Loop Modes.”
You can add a timer to multiple input modes. While running in the designated mode, the receiver causes the timer to fire on or after its scheduled fire date. Upon firing, the timer invokes its associated handler routine, which is a selector on a designated object.
The receiver retains aTimer. To remove a timer from all run loop modes on which it is installed, send an invalidate
message to the timer.
NSRunLoop.h
Cancels the sending of a previously scheduled message.
- (void)cancelPerformSelector:(SEL)aSelector target:(id)target argument:(id)anArgument
The previously-specified selector.
The previously-specified target.
The previously-specified argument.
You can use this method to cancel a message previously scheduled using the performSelector:target:argument:order:modes:
method. The parameters identify the message you want to cancel and must match those originally specified when the selector was scheduled. This method removes the perform request from all modes of the run loop.
NSRunLoop.h
Cancels all outstanding ordered performs scheduled with a given target.
- (void)cancelPerformSelectorsWithTarget:(id)target
The previously-specified target.
This method cancels the previously scheduled messages associated with the target, ignoring the selector and argument of the scheduled operation. This is in contrast to cancelPerformSelector:target:argument:
, which requires you to match the selector and argument as well as the target. This method removes the perform requests for the object from all modes of the run loop.
NSRunLoop.h
Returns the receiver's current input mode.
- (NSString *)currentMode
The receiver's current input mode. This method returns the current input mode only while the receiver is running; otherwise, it returns nil
.
The current mode is set by the methods that run the run loop, such as acceptInputForMode:beforeDate:
and runMode:beforeDate:
.
NSRunLoop.h
Returns the receiver's underlying CFRunLoop Reference object.
- (CFRunLoopRef)getCFRunLoop
The receiver's underlying CFRunLoop Reference object.
You can use the returned run loop to configure the current run loop using Core Foundation function calls. For example, you might use this function to set up a run loop observer.
NSRunLoop.h
Performs one pass through the run loop in the specified mode and returns the date at which the next timer is scheduled to fire.
- (NSDate *)limitDateForMode:(NSString *)mode
The run loop mode to search. You may specify custom modes or use one of the modes listed in “Run Loop Modes.”
The date at which the next timer is scheduled to fire, or nil
if there are no input sources for this mode.
The run loop is entered with an immediate timeout, so the run loop does not block, waiting for input, if no input sources need processing.
NSRunLoop.h
Schedules the sending of a message on the current run loop.
- (void)performSelector:(SEL)aSelector target:(id)target argument:(id)anArgument order:(NSUInteger)order modes:(NSArray *)modes
A selector that identifies the method to invoke. This method should not have a significant return value and should take a single argument of type id.
The object that defines the selector in aSelector.
The argument to pass to the method when it is invoked. Pass nil
if the method does not take an argument.
The priority for the message. If multiple messages are scheduled, the messages with a lower order value are sent before messages with a higher order value.
An array of input modes for which the message may be sent. You may specify custom modes or use one of the modes listed in “Run Loop Modes.”
This method sets up a timer to perform the aSelector message on the current thread’s run loop at the start of the next run loop iteration. The timer is configured to run in the modes specified by the modes parameter. When the timer fires, the thread attempts to dequeue the message from the run loop and perform the selector. It succeeds if the run loop is running and in one of the specified modes; otherwise, the timer waits until the run loop is in one of those modes.
This method returns before the aSelector message is sent. The receiver retains the target and anArgument objects until the timer for the selector fires, and then releases them as part of its cleanup.
Use this method if you want multiple messages to be sent after the current event has been processed and you want to make sure these messages are sent in a certain order.
NSRunLoop.h
Removes a port from the specified input mode of the run loop.
- (void)removePort:(NSPort *)aPort forMode:(NSString *)mode
The port to remove from the receiver.
The mode from which to remove aPort. You may specify a custom mode or use one of the modes listed in “Run Loop Modes.”
If you added the port to multiple input modes, you must remove it from each mode separately.
NSRunLoop.h
Puts the receiver into a permanent loop, during which time it processes data from all attached input sources.
- (void)run
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:
. In other words, this method effectively begins an infinite loop that processes data from the run loop’s input sources and timers.
Manually removing all known input sources and timers from the run loop is not a guarantee that the run loop will exit. Mac 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.
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:
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.
NSRunLoop.h
Runs the loop once, blocking for input in the specified mode until a given date.
- (BOOL)runMode:(NSString *)mode beforeDate:(NSDate *)limitDate
The mode in which to run. You may specify custom modes or use one of the modes listed in “Run Loop Modes.”
The date until which to block.
NO
without starting the run loop if there are no input sources in mode; otherwise YES
.
If no input sources or timers are attached to the run loop, this method exits immediately; otherwise, it returns after either the first input source is processed or limitDate is reached. Manually removing all known input sources and timers from the run loop is not a guarantee that the run loop will exit. Mac 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
NSRunLoop.h
Runs the loop until the specified date, during which time it processes data from all attached input sources.
- (void)runUntilDate:(NSDate *)limitDate
The date up until which to run.
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.
Manually removing all known input sources and timers from the run loop is not a guarantee that the run loop will exit. Mac 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.
NSRunLoop.h
NSRunLoop
defines the following run loop mode.
extern NSString *NSDefaultRunLoopMode;
NSDefaultRunLoopMode
The mode to deal with input sources other than NSConnection
objects.
This is the most commonly used run-loop mode.
Available in Mac OS X v10.0 and later.
Declared in NSRunLoop.h
.
NSRunLoopCommonModes
Objects added to a run loop using this value as the mode are monitored by all run loop modes that have been declared as a member of the set of “common" modes; see the description of CFRunLoopAddCommonMode
for details.
Available in Mac OS X v10.5 and later.
Declared in NSRunLoop.h
.
Foundation/NSRunLoop.h
Additional run loop modes are defined by NSConnection
and NSApplication
.
NSConnectionReplyMode | Use this mode to indicate NSConnection objects waiting for replies. Defined in the Foundation/NSConnection.h header file. You rarely need to use this mode. |
NSModalPanelRunLoopMode | A run loop should be set to this mode when waiting for input from a modal panel, such as NSSavePanel or NSOpenPanel. |
NSEventTrackingRunLoopMode | A run loop should be set to this mode when tracking events modally, such as a mouse-dragging loop. |
© 2008 Apple Inc. All Rights Reserved. (Last updated: 2008-02-08)