The programmatic interface to objects that manage input sources.


class RunLoop : NSObject


A RunLoop object processes input for sources such as mouse and keyboard events from the window system, Port objects, and NSConnection objects. A RunLoop object also processes Timer events.

Your application neither creates or explicitly manages RunLoop objects. Each Thread object—including the application’s main thread—has an RunLoop 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 current.

Note that from the perspective of RunLoop, Timer 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.


Accessing Run Loops and Modes

class var current: RunLoop

Returns the run loop for the current thread.

var currentMode: RunLoop.Mode?

The receiver's current input mode.

func limitDate(forMode: RunLoop.Mode) -> Date?

Performs one pass through the run loop in the specified mode and returns the date at which the next timer is scheduled to fire.

class var main: RunLoop

Returns the run loop of the main thread.

func getCFRunLoop() -> CFRunLoop

Returns the receiver's underlying CFRunLoop object.

Managing Timers

func add(Timer, forMode: RunLoop.Mode)

Registers a given timer with a given input mode.

Managing Ports

func add(Port, forMode: RunLoop.Mode)

Adds a port as an input source to the specified mode of the run loop.

func remove(Port, forMode: RunLoop.Mode)

Removes a port from the specified input mode of the run loop.

Running a Loop

func run()

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

func run(mode: RunLoop.Mode, before: Date) -> Bool

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

func run(until: Date)

Runs the loop until the specified date, during which time it processes data from all attached input sources.

func acceptInput(forMode: RunLoop.Mode, before: Date)

Runs the loop once or until the specified date, accepting input only for the specified mode.

Scheduling and Canceling Messages

func cancelPerform(Selector, target: Any, argument: Any?)

Cancels the sending of a previously scheduled message.

func cancelPerformSelectors(withTarget: Any)

Cancels all outstanding ordered performs scheduled with a given target.

Scheduling Combine Publishers

func schedule(options: RunLoop.SchedulerOptions?, () -> Void)

Performs the action at some time after the specified date, using the scheduler’s minimum tolerance.

func schedule(after: RunLoop.SchedulerTimeType, tolerance: RunLoop.SchedulerTimeType.Stride, options: RunLoop.SchedulerOptions?, () -> Void)

Performs the action at some time after the specified date, using the specified tolerance and options.

var minimumTolerance: RunLoop.SchedulerTimeType.Stride

The minimum tolerance allowed by the run loop scheduler.

var now: RunLoop.SchedulerTimeType

The run loop scheduler’s definition of the current moment in time.

struct RunLoop.SchedulerTimeType

The scheduler time type used by the run loop.

struct RunLoop.SchedulerOptions

A set of options that affect the operation of the run loop scheduler.


Run Loop Modes

NSRunLoop defines the following run loop mode.


Inherits From

See Also

Run Loop Scheduling

class Timer

A timer that fires after a certain time interval has elapsed, sending a specified message to a target object.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software