NSRunLoop官方文档翻译

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.

NSRunLoop类声明了管理输入源(input sources)的可编程接口。一个NSRunLoop对象处理像鼠标和键盘事件这样的来自于窗口系统( window system)、NSPort对象和 NSConnection对象的输入源( input for source)。NSRunLoop也可以处理NSTimer事件。


Your application cannot 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.

你的应用不能创建或管理NSRunLoop对象。每一个NSThread对象,包括应用的主线程(main thread)都有一个可以在需要时自动创建的NSRunLoop对象。如果你需要访问当前线程的run loop,你可以调用类方法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.

从NSRunloop的角度来看,NSTimer对象不是输入(input)而是一种特殊的类型,这意味着当NSTimer对象被触发(fire)时,不会导致run loop返回(return)。


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.

注意
NSRunLoop类一般不被认为是线程安全的,其方法只应该在当前线程的上下文中调用。你永远不要去试图调用另一个线程中正在运行的NSRunLoop对象,这样做将会导致不可预期的结果。


Accessing Run Loops and Modes
访问Run Loops和

  • currentRunLoop
    Returns the NSRunLoop object for the current thread.

Declaration
OBJECTIVE-C

  • (NSRunLoop *)currentRunLoop
    Return Value
    The NSRunLoop object for the current thread.

Discussion
If a run loop does not yet exist for the thread, one is created and returned.

Availability
Available in iOS 2.0 and later.
See Also
– currentMode

currentMode
Property
The receiver's current input mode. (read-only)

Declaration
OBJECTIVE-C
@property(readonly, copy) NSString *currentMode
Discussion
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:.

Availability
Available in iOS 2.0 and later.
See Also

  • currentRunLoop
    – limitDateForMode:
    – run
    – runUntilDate:
  • limitDateForMode:
    Performs one pass through the run loop in the specified mode and returns the date at which the next timer is scheduled to fire.

Declaration
OBJECTIVE-C

  • (NSDate *)limitDateForMode:(NSString *)mode
    Parameters
    mode
    The run loop mode to search. You may specify custom modes or use one of the modes listed in Run Loop Modes.
    Return Value
    The date at which the next timer is scheduled to fire, or nil if there are no input sources for this mode.

Discussion
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.

Availability
Available in iOS 2.0 and later.

  • mainRunLoop
    Returns the run loop of the main thread.

Declaration
OBJECTIVE-C

  • (NSRunLoop *)mainRunLoop
    Return Value
    An object representing the main thread’s run loop.

Availability
Available in iOS 2.0 and later.

  • getCFRunLoop
    Returns the receiver's underlying CFRunLoop Reference object.

Declaration
OBJECTIVE-C

  • (CFRunLoopRef)getCFRunLoop
    Return Value
    The receiver's underlying CFRunLoop Reference object.

Discussion
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.

Availability
Available in iOS 2.0 and later.
Managing Timers

  • addTimer:forMode:
    Registers a given timer with a given input mode.

Declaration
OBJECTIVE-C

  • (void)addTimer:(NSTimer *)aTimer forMode:(NSString *)mode
    Parameters
    aTimer
    The timer to register with the receiver.
    mode
    The mode in which to add aTimer. You may specify a custom mode or use one of the modes listed in Run Loop Modes.
    Discussion
    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.

Availability
Available in iOS 2.0 and later.
Managing Ports

  • addPort:forMode:
    Adds a port as an input source to the specified mode of the run loop.

Declaration
OBJECTIVE-C

  • (void)addPort:(NSPort *)aPort forMode:(NSString *)mode
    Parameters
    aPort
    The port to add to the receiver.
    mode
    The mode in which to add aPort. You may specify a custom mode or use one of the modes listed in Run Loop Modes.
    Discussion
    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.

Availability
Available in iOS 2.0 and later.
See Also
– removePort:forMode:

  • removePort:forMode:
    Removes a port from the specified input mode of the run loop.

Declaration
OBJECTIVE-C

  • (void)removePort:(NSPort *)aPort forMode:(NSString *)mode
    Parameters
    aPort
    The port to remove from the receiver.
    mode
    The mode from which to remove aPort. You may specify a custom mode or use one of the modes listed in Run Loop Modes.
    Discussion
    If you added the port to multiple input modes, you must remove it from each mode separately.

Availability
Available in iOS 2.0 and later.
See Also
– addPort:forMode:

Running a Loop

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

Declaration
OBJECTIVE-C

  • (void)run
    Discussion
    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. 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.

Availability
Available in iOS 2.0 and later.
See Also
– runUntilDate:

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

Declaration
OBJECTIVE-C

  • (BOOL)runMode:(NSString *)mode beforeDate:(NSDate *)limitDate
    Parameters
    mode
    The mode in which to run. You may specify custom modes or use one of the modes listed in Run Loop Modes.
    limitDate
    The date until which to block.
    Return Value
    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. 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

Availability
Available in iOS 2.0 and later.
See Also
– run
– runUntilDate:

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

Declaration
OBJECTIVE-C

  • (void)runUntilDate:(NSDate *)limitDate
    Parameters
    limitDate
    The date up until which to run.
    Discussion
    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. 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.

Availability
Available in iOS 2.0 and later.
See Also
– run

  • acceptInputForMode:beforeDate:
    Runs the loop once or until the specified date, accepting input only for the specified mode.

Declaration
OBJECTIVE-C

  • (void)acceptInputForMode:(NSString *)mode beforeDate:(NSDate *)limitDate
    Parameters
    mode
    The mode in which to run. You may specify custom modes or use one of the modes listed in Run Loop Modes.
    limitDate
    The date up until which to run.
    Discussion
    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. 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.

Availability
Available in iOS 2.0 and later.
See Also
– runMode:beforeDate:

Scheduling and Canceling Messages

  • performSelector:target:argument:order:modes:
    Schedules the sending of a message on the current run loop.

Declaration
OBJECTIVE-C

  • (void)performSelector:(SEL)aSelector target:(id)target argument:(id)anArgument order:(NSUInteger)order modes:(NSArray<NSString *> *)modes
    Parameters
    aSelector
    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.
    target
    The object that defines the selector in aSelector.
    anArgument
    The argument to pass to the method when it is invoked. Pass nil if the method does not take an argument.
    order
    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.
    modes
    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.
    Discussion
    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.

Availability
Available in iOS 2.0 and later.
See Also
– cancelPerformSelector:target:argument:

  • cancelPerformSelector:target:argument:
    Cancels the sending of a previously scheduled message.

Declaration
OBJECTIVE-C

  • (void)cancelPerformSelector:(SEL)aSelector target:(id)target argument:(id)anArgument
    Parameters
    aSelector
    The previously-specified selector.
    target
    The previously-specified target.
    anArgument
    The previously-specified argument.
    Discussion
    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.

Availability
Available in iOS 2.0 and later.

  • cancelPerformSelectorsWithTarget:
    Cancels all outstanding ordered performs scheduled with a given target.

Declaration
OBJECTIVE-C

  • (void)cancelPerformSelectorsWithTarget:(id)target
    Parameters
    target
    The previously-specified target.
    Discussion
    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.

Availability
Available in iOS 2.0 and later.
Constants
Run Loop Modes
NSRunLoop defines the following run loop mode.

Declaration
OBJECTIVE-C
extern NSString* const NSDefaultRunLoopMode;
extern NSString* const NSRunLoopCommonModes;
Constants
NSDefaultRunLoopMode
The mode to deal with input sources other than NSConnection objects.

This is the most commonly used run-loop mode.

Available in iOS 2.0 and later.
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 iOS 2.0 and later.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,340评论 5 467
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,762评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,329评论 0 329
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,678评论 1 270
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,583评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 47,995评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,493评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,145评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,293评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,250评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,267评论 1 328
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,973评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,556评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,648评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,873评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,257评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,809评论 2 339

推荐阅读更多精彩内容