Swift CFRunLoop

来源:互联网 发布:金万维免费域名 编辑:程序博客网 时间:2024/05/18 02:24

A CFRunLoop object monitors sources of input to a task and dispatches control when they become ready for processing. Examples of input sources might include user input devices, network connections, periodic or time-delayed events, and asynchronous callbacks.

CFRunLoop对象监控输入任务的来源和分派控制当他们准备好处理。输入源的例子可能包括用户输入设备、网络连接、周期或时滞事件和异步回调。

Three types of objects can be monitored by a run loop: sources (CFRunLoopSource), timers (CFRunLoopTimer), and observers (CFRunLoopObserver). To receive callbacks when these objects need processing, you must first place these objects into a run loop withCFRunLoopAddSource(_:_:_:),CFRunLoopAddTimer(_:_:_:), orCFRunLoopAddObserver(_:_:_:). You can later remove an object from the run loop (or invalidate it) to stop receiving its callback.

三种类型的对象可以被监视的运行循环:来源(CFRunLoopSource),计时器(CFRunLoopTimer)和观察员(CFRunLoopObserver)。接收回调函数当这些对象需要处理时,您必须首先将这些对象与CFRunLoopAddSource运行循环(_:_:_:),CFRunLoopAddTimer(_:_:_:),或CFRunLoopAddObserver(_:_:_:)。你可以稍后从运行循环中删除一个对象(或无效)停止接收回调。

Each source, timer, and observer added to a run loop must be associated with one or more run loop modes. Modes determine what events are processed by the run loop during a given iteration. Each time the run loop executes, it does so in a specific mode. While in that mode, the run loop processes only the events associated with sources, timers, and observers associated with that mode. You assign most sources to the default run loop mode (designated by thedefaultMode constant), which is used to process events when the application (or thread) is idle. However, the system defines other modes and may execute the run loop in those other modes to limit which sources, timers, and observers are processed. Because run-loop modes are simply specified as strings, you can also define your own custom modes to limit the processing of events

每个源、计时器和观察者添加到运行循环必须与一个或多个运行循环模式。模式确定事件处理的运行循环在给定迭代。每一次运行循环执行时,它在一个特定的模式。而在这种模式下,只运行循环过程与资源相关的事件,计时器,观察者与模式。你最分配资源来默认的运行循环模式(defaultMode常数指定的),用于处理事件时,应用程序(或线程)是空闲的。然而,系统定义了其他模式,可能在其他执行运行循环模式限制来源,计时器,观察人士处理。因为运行循环模式仅仅是指定为字符串,您还可以定义自己的自定义模式限制事件的处理

Core Foundation defines a special pseudo-mode, called the common modes, that allow you to associate more than one mode with a given source, timer, or observer. To specify the common modes, use thecommonModes constant for the mode when configuring the object. Each run loop has its own independent set of common modes and the default mode (defaultMode) is always a member of the set. To add a mode to the set of common modes, use theCFRunLoopAddCommonMode(_:_:) function.

定义了一个特殊的pseudo-mode核心基础,称为公共模式,允许你将多个模式与一个给定的源,定时器或观察者。指定的共同模式,使用的commonModes常数模式在配置对象。每个运行循环都有自己的独立的常见模式,默认模式(defaultMode)总是组的一员。添加一个模式的常见模式,使用CFRunLoopAddCommonMode(_:_:)功能。

There is exactly one run loop per thread. You neither create nor destroy a thread’s run loop. Core Foundation automatically creates it for you as needed. You obtain the current thread’s run loop withCFRunLoopGetCurrent(). CallCFRunLoopRun() to run the current thread’s run loop in the default mode until the run loop is stopped withCFRunLoopStop(_:). You can also callCFRunLoopRunInMode(_:_:_:) to run the current thread’s run loop in a specified mode for a set period of time (or until the run loop is stopped). A run loop can only run if the requested mode has at least one source or timer to monitor.

有一个循环每个线程运行。你创建和销毁线程的运行循环。为您自动创建所需的核心基础。您获得当前线程的运行循环CFRunLoopGetCurrent()。CFRunLoopRun()的调用当前线程的运行在默认模式下运行循环,直到停止运行循环与CFRunLoopStop(_:)。你也可以叫CFRunLoopRunInMode(_:_:_:)运行指定的当前线程的运行循环模式设置时间(或者直到运行循环停止)。运行循环只能运行如果请求的模式至少有一个源或计时器来监控。

Run loops can be run recursively. You can call CFRunLoopRun() or CFRunLoopRunInMode(_:_:_:) from within any run loop callout and create nested run loop activations on the current thread’s call stack. You are not restricted in which modes you can run from within a callout. You can create another run loop activation running in any available run loop mode, including any modes already running higher in the call stack.

运行循环可以递归地运行。你可以叫CFRunLoopRun()或CFRunLoopRunInMode(_:_:_:)在任何运行循环调出并创建嵌套循环运行激活当前线程的调用堆栈。你不受限制的模式,您可以运行在调出。您可以创建另一个运行循环激活运行在任何可用的运行循环模式,包括任何模式已经运行更高的调用堆栈。

Cocoa applications build upon CFRunLoop to implement their own higher-level event loop. When writing an application, you can add your sources, timers, and observers to their run loop objects and modes. Your objects will then get monitored as part of the regular application event loop. Use the getCFRunLoop() method of RunLoop to obtain the correspondingCFRunLoop type. In Carbon applications, use theGetCFRunLoopFromEventLoop(_:) function.

Cocoa应用程序建立CFRunLoop实现自己的高级事件循环。在编写应用程序时,您可以添加您的来源,计时器,观察员运行循环对象和模式。对象将会得到监控作为常规应用程序事件循环的一部分。使用getCFRunLoop RunLoop()方法来获取相应的CFRunLoop类型。在碳的应用程序中,使用GetCFRunLoopFromEventLoop(_:)功能。

For more information about how run loops behave, see Run Loops in Threading Programming Guide.

如何运行循环行为的更多信息,参见运行循环线程编程指南。

如果转载请注明转于:AirZilong的博客


1 0
原创粉丝点击