runloop源

Input Sources

輸入源

Input sources deliver events asynchronously to your threads. The source of the event depends on the type of the input source, which is generally one of two categories. Port-based input sources monitor your application’s Mach ports. Custom input sources monitor custom sources of events. As far as your run loop is concerned, it should not matter whether an input source is port-based or custom. The system typically implements input sources of both types that you can use as is. The only difference between the two sources is how they are signaled. Port-based sources are signaled automatically by the kernel, and custom sources must be signaled manually from another thread.

輸入源會異步的傳遞事件給你的線程。事件的來源取決于輸入源,這通常是兩個類別之一?;诙丝诘妮斎朐幢O(jiān)聽你程序的硬件端口。自定義輸入源監(jiān)聽自定義事件源。就runloop而言,輸入源是基于端口還是自定義并不重要。系統(tǒng)通常輸入源的各種類型,你可以使用。僅有的不同是如何發(fā)出信號?;诙丝谑怯蓛群税l(fā)出信號。自定義輸入源必須由另外一個線程發(fā)出信號。

When you create an input source, you assign it to one or more modes of your run loop. Modes affect which input sources are monitored at any given moment. Most of the time, you run the run loop in the default mode, but you can specify custom modes too. If an input source is not in the currently monitored mode, any events it generates are held until the run loop runs in the correct mode.

當你創(chuàng)建一個輸入源的時候,你將給source分配你runloop里邊的一種或多種mode。mode會影響在給定時刻哪種source會被監(jiān)聽。大多數情況下,你在默認模式下運行你的runloop。但是你也可以專門定義你的mode。如果一個輸入源不在當前監(jiān)聽的mode,那么這個source生成的任何事件將保持,直到runloop以正確的mode運行。

注釋:簡單來說就是,runloop會以不同的mode運行。在不同的mode下,source會發(fā)送不同的消息給程序進行處理。如果不在runloop不在對應的mode下運行,就不發(fā)消息。例如滑動時,程序是在runloop跟蹤模式下運行,那么默認mode對應的source就不會發(fā)消息給程序執(zhí)行。

The following sections describe some of the input sources.

下邊幾個部分描述幾種輸入源。

Port-Based Sources

基于端口的源

Cocoa and Core Foundation provide built-in support for creating port-based input sources using port-related objects and functions. For example, in Cocoa, you never have to create an input source directly at all. You simply create a port object and use the methods ofNSPortto add that port to the run loop. The port object handles the creation and configuration of the needed input source for you.

cocoa和cocoa基礎架構cocoa用端口對象和函數為創(chuàng)建端口輸入源提供支持。例如,在cocoa中,你從來不需要直接創(chuàng)建創(chuàng)建輸入源。你只需要單單創(chuàng)建一個端口對象,添加它到runloop中即可。端口對象幫你創(chuàng)建和配置所需的輸入源。

In Core Foundation, you must manually create both the port and its run loop source. In both cases, you use the functions associated with the port opaque type (CFMachPortRef,CFMessagePortRef, orCFSocketRef) to create the appropriate objects.

在核心框架中,你必須手動創(chuàng)建端口和它的runloop源。在這兩種情況下,你使用和端口類型相關的函數來創(chuàng)建合適的對象。

Custom Input Sources

自定義輸入源

To create a custom input source, you must use the functions associated with the?CFRunLoopSourceRef?opaque type in Core Foundation. You configure a custom input source using several callback functions. Core Foundation calls these functions at different points to configure the source, handle any incoming events, and tear down the source when it is removed from the run loop.

在核心框架下,為了創(chuàng)建一個自定義輸入源,你必須結合與CFRunLoopSourceRef相關的函數。你要用幾個回調函數來配置輸入源。核心基礎框架會在不同的點調用這些函數來配置這些源,處理任何將要發(fā)生的事件。并在runloop刪除源時將其卸載。

In addition to defining the behavior of the custom source when an event arrives, you must also define the event delivery mechanism. This part of the source runs on a separate thread and is responsible for providing the input source with its data and for signaling it when that data is ready for processing. The event delivery mechanism is up to you but need not be overly complex.

當事件到達的時候除了要定義自定義源的行為,你還必須定義事件傳遞機制。source的這部分運行在單獨的線程上,源負責向輸入源提供數據,并在數據處理好的時候發(fā)送給它。事件傳遞機制取決于你,沒必要過于復雜。

Cocoa Perform Selector Sources

cocoa執(zhí)行selector源

In addition to port-based sources, Cocoa defines a custom input source that allows you to perform a selector on any thread. Like a port-based source, perform selector requests are serialized on the target thread, alleviating many of the synchronization problems that might occur with multiple methods being run on one thread. Unlike a port-based source, a perform selector source removes itself from the run loop after it performs its selector.

除了基于端口的源,cocoa還定義了一個自定義輸入源,來允許你在任何線程上執(zhí)行方法。像基于端口的源一樣,執(zhí)行選擇器請求在目標線程上被序列化,減輕了多個方法在一個線程上運行可能發(fā)生的許多同步問題。與基于端口的源不同,執(zhí)行選擇器源會在執(zhí)行完后把它從runloop中移除。

When performing a selector on another thread, the target thread must have an active run loop. For threads you create, this means waiting until your code explicitly starts the run loop. Because the main thread starts its own run loop, however, you can begin issuing calls on that thread as soon as the application calls the applicationDidFinishLaunching:method of the application delegate. The run loop processes all queued perform selector calls each time through the loop, rather than processing one during each loop iteration.

當在另一個線程上執(zhí)行selector的時候,目標線程必須有一個活躍的runloop。對于你創(chuàng)建的線程,這意味著它會等到你的代碼中顯示的開始runloop。由于主線程啟動自己的runloop。然而,一旦程序調用了didfinish方法,就可以在線程上發(fā)生調用。runloop在循環(huán)中每次執(zhí)行所有的選擇器,不是每次循環(huán)執(zhí)行一個。

Table 3-2 lists the methods defined onNSObject that can be used to perform selectors on other threads. Because these methods are declared onNSObject, you can use them from any threads where you have access to Objective-C objects, including POSIX threads. These methods do not actually create a new thread to perform the selector.

下表列出了NSObject上定義了在其他線程上可以用來執(zhí)行的方法。由于這些方法定義在NSObject上,你可以應用他們在任何線程上,只要你可以訪問到oc對象,包括POSIX。線程。這些方法實際上并沒有創(chuàng)建一個新的線程。


方法源

Timer Sources

定時器源

Timer sources deliver events synchronously to your threads at a preset time in the future. Timers are a way for a thread to notify itself to do something. For example, a search field could use a timer to initiate an automatic search once a certain amount of time has passed between successive key strokes from the user. The use of this delay time gives the user a chance to type as much of the desired search string as possible before beginning the search.

定時器源會在預設的將來同步發(fā)送時間到你的線程。對于線程來說,定時器是一種通知自己做事的一種方式。例如,用戶輸入一段時間,你就可以自動搜索進行搜索。延遲時間的應用可以給用戶在開始搜索之前,有機會得到盡可能想要搜索的字符串。

Although it generates time-based notifications, a timer is not a real-time mechanism. Like input sources, timers are associated with specific modes of your run loop. If a timer is not in the mode currently being monitored by the run loop, it does not fire until you run the run loop in one of the timer’s supported modes. Similarly, if a timer fires when the run loop is in the middle of executing a handler routine, the timer waits until the next time through the run loop to its handler routine. If the run loop is not running at all, the timer never fires.

盡管定時源會生成基于定時器的通知,但是定時器不是一種實時的機制。與輸入源一樣,定時器也和runloop的模式相結合。如果一個定時器不屬于當前循環(huán)監(jiān)聽的模式,直到這個runloop運行定時器支持的模式才會啟動。相似的,如果當前的runloop正在處理其他事情,定時器觸發(fā),那么定時器會等到runloop下一次處理程序時。如果runloop沒有啟動,那么定時器從來都不會開啟。

You can configure timers to generate events only once or repeatedly. A repeating timer reschedules itself automatically based on the scheduled firing time, not the actual firing time. For example, if a timer is scheduled to fire at a particular time and every 5 seconds after that, the scheduled firing time will always fall on the original 5 second time intervals, even if the actual firing time gets delayed. If the firing time is delayed so much that it misses one or more of the scheduled firing times, the timer is fired only once for the missed time period. After firing for the missed period, the timer is rescheduled for the next scheduled firing time.

你可以配置定時器一次或者多次執(zhí)行。重復執(zhí)行會自動安排執(zhí)行按照設定的時間重新調度,不是實際開啟時間。例如,如果一個定時器運行在特定時間和之后每隔5秒。那么后邊盡管后邊實際開啟時間延誤了,但是預定時間依然是在原始的基礎上隔5秒。如果延誤太多,那么定時器會錯過一次或多次,僅僅運行錯過時期的一次。之后,將重新安排下一次的時間。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容