簡介
Run loops are part of the fundamental infrastructure associated with threads. A?run loop?is an event processing loop that you use to schedule work and coordinate the receipt of incoming events. The purpose of a run loop is to keep your thread busy when there is work to do and put your thread to sleep when there is none.
runloop是與線程關(guān)聯(lián)的基礎(chǔ)架構(gòu)。runloop是一個事件處理循環(huán)矩乐,用來安排工作以及即將接收到的事件龄句。runloop是為了保持你的線程處于busy狀態(tài),有事情處理就活躍散罕,沒有事情處理就處于睡眠狀態(tài)分歇。
Run loop management is not entirely automatic. You must still design your thread’s code to start the run loop at appropriate times and respond to incoming events. Both Cocoa and Core Foundation provide?run loop objectsto help you configure and manage your thread’s run loop. Your application does not need to create these objects explicitly; each thread, including the application’s main thread, has an associated run loop object. Only secondary threads need to run their run loop explicitly, however. The app frameworks automatically set up and run the run loop on the main thread as part of the application startup process.
runloop的管理部完全是自動的。你必須設(shè)計(jì)你的thread的代碼在合適的時機(jī)開啟線欧漱。cocoa和cocoa基礎(chǔ)框架提供了runloop對象來幫助你配置和管理你線程的runloop职抡。你的程序并不需要顯示的創(chuàng)建這些這些對象,每個線程都包含了一個runloop對象误甚。只有輔助線程需要顯示的跑runloop缚甩。然而,作為應(yīng)用程序啟動過程的一部分窑邦,app框架自動創(chuàng)建和在主線程上跑runloop擅威。
The following sections provide more information about run loops and how you configure them for your application. For additional information about run loop objects, see?NSRunLoop Class Reference?and?CFRunLoop Reference.
下邊部分提供了更多信息關(guān)于runloop以及如何為你的應(yīng)用程序配置他們,下邊是關(guān)于runloop對象的更多信息冈钦。
Anatomy of a Run Loop
runloop的運(yùn)行原理
A run loop is very much like its name sounds. It is a loop your thread enters and uses to run event handlers in response to incoming events. Your code provides the control statements used to implement the actual loop portion of the run loop—in other words, your code provides the while or for loop that drives the run loop. Within your loop, you use a run loop object to "run” the event-processing code that receives events and calls the installed handlers.
runloop非常像像他的名字郊丛。它是通過線程進(jìn)入,用來處理接下來需要響應(yīng)事件的循環(huán)瞧筛。你的代碼提供了用于實(shí)現(xiàn)runloop的的控制語句厉熟。換句話說,你的代碼提供了while或者for語句來運(yùn)行runloop较幌。在循環(huán)中揍瑟,你用runloop對象來跑事件進(jìn)程的代碼,這些代碼用來接收事件和調(diào)用已經(jīng)安裝的程序乍炉。
A run loop receives events from two different types of sources.Input sources?deliver asynchronous events, usually messages from another thread or from a different application.Timer sources?deliver synchronous events, occurring at a scheduled time or repeating interval. Both types of source use an application-specific handler routine to process the event when it arrives.
runloop接收從兩種不同來源的事件绢片。輸入源用來處理異步事件,通常這些消息來源于另一個線程或其他的程序岛琼。時間源提供同步事件底循,發(fā)生在預(yù)定時間或者重復(fù)間隔。當(dāng)事件發(fā)生時衷恭,兩種源使用一種應(yīng)用程序的特定處理程序來處理此叠。
Figure 3-1shows the conceptual structure of a run loop and a variety of sources. The input sources deliver asynchronous events to the corresponding handlers and cause the?runUntilDate:method (called on the thread’s associatedNSRunLoopobject) to exit. Timer sources deliver events to their handler routines but do not cause the run loop to exit.
下圖顯示了runloop的結(jié)構(gòu)和何種源纯续,輸入源將異步事件傳遞傳遞給相應(yīng)的處理程序随珠,并且引起runUntilDate:方法終止灭袁。時間源會將時間傳遞給他們的處理機(jī)制,但是不退使runloop退出窗看。
In addition to handling sources of input, run loops also generate notifications about the run loop’s behavior. Registered?run-loop observers?can receive these notifications and use them to do additional processing on the thread. You use Core Foundation to install run-loop observers on your threads.
除了處理輸入源之外茸歧,runloop還會生成關(guān)于runloop行為的通知。注冊為runloop的觀察者可以接收到這些通知显沈。用他們在線程上做額外的處理软瞎。你應(yīng)用核心基礎(chǔ)框架在線程上安裝runloop觀察者。
The following sections provide more information about the components of a run loop and the modes in which they operate. They also describe the notifications that are generated at different times during the handling of events.
下邊幾個部分提供了runloop組件的更多信息拉讯,以及他們操作的那些模式涤浇。它們還描述了處理事件的不同時期生成的通知。
Run Loop Modes
runloop模式
A?run loop mode?is a collection of input sources and timers to be monitored and a collection of run loop observers to be notified. Each time you run your run loop, you specify (either explicitly or implicitly) a particular “mode” in which to run. During that pass of the run loop, only sources associated with that mode are monitored and allowed to deliver their events. (Similarly, only observers associated with that mode are notified of the run loop’s progress.) Sources associated with other modes hold on to any new events until subsequent passes through the loop in the appropriate mode.
runloop模式是一個輸入源的集合魔慷,定時器會被監(jiān)測只锭,runloop觀察者的集合將會受到通知。每次啟動runloop的時候院尔,你都會指定(顯示或者隱示)跑的特定模式蜻展。在runloop通過期間,只有與該模式相關(guān)的源會被監(jiān)聽邀摆,允許處理事件纵顾。(相似的,只有與該模式結(jié)合的觀察者會被通知runloop的進(jìn)度)栋盹。與其他模式相結(jié)合的源會等待一些新的事件施逾,直到后續(xù)相應(yīng)的模式通過這個runloop。
In your code, you identify modes by name. Both Cocoa and Core Foundation define a default mode and several commonly used modes, along with strings for specifying those modes in your code. You can define custom modes by simply specifying a custom string for the mode name. Although the names you assign to custom modes are arbitrary, the contents of those modes are not. You must be sure to add one or more input sources, timers, or run-loop observers to any modes you create for them to be useful.
在你的代碼中贞盯,你區(qū)分模式通過名字音念。Apple定義了默認(rèn)模式和幾種普通模式,在和特定的字符串綁定一起來使用躏敢。你可以通過字符串自定義模式名字闷愤。盡管你可以隨意的定義模式的名字,但是模式的內(nèi)容卻不能隨意定義件余。你必須確保添加一種或多種輸入源讥脐,定時器,或者觀察者帶模式中使其有用啼器。
You use modes to filter out events from unwanted sources during a particular pass through your run loop. Most of the time, you will want to run your run loop in the system-defined “default” mode. A modal panel, however, might run in the “modal” mode. While in this mode, only sources relevant to the modal panel would deliver events to the thread. For secondary threads, you might use custom modes to prevent low-priority sources from delivering events during time-critical operations.
你可以使用模式在特定通過你的循環(huán)時旬渠,過濾不想要的事件。大多數(shù)情況下端壳,你想要運(yùn)行你的runloop在系統(tǒng)的默認(rèn)模式告丢。然而,一種形式的面板损谦,可能以模態(tài)形式運(yùn)行岖免。當(dāng)在這種模式的情況下岳颇,只有和模式面板相關(guān)的source才會將事件傳遞給線程。對于輔助線程颅湘,你可能用自定義的模式來阻止低優(yōu)先級的source在時間緊迫的情況下傳遞事件话侧。
Note:Modes discriminate based on the source of the event, not the type of the event. For example, you would not use modes to match only mouse-down events or only keyboard events. You could use modes to listen to a different set of ports, suspend timers temporarily, or otherwise change the sources and run loop observers currently being monitored.
注意:模式根據(jù)事件源來區(qū)分不是事件的類型。例如:你不會用模式來匹配僅有的鼠標(biāo)事件或鍵盤事件闯参。你可以用模式來監(jiān)聽不同的端口瞻鹏,臨時暫停定時器,或者改變當(dāng)前被監(jiān)聽的源和runloop的觀察者鹿寨。
Table 3-1 lists the standard modes defined by Cocoa and Core Foundation along with a description of when you use that mode. The name column lists the actual constants you use to specify the mode in your code.
下表列出了cocoa定義的幾種標(biāo)準(zhǔn)模式新博,以及使用模式的描述。名稱列列出了在特定模式下使用的常量脚草。
默認(rèn)模式:
默認(rèn)模式使用操作最多的一個模式叭披。大多數(shù)時候,你應(yīng)該應(yīng)用這個模式來開啟的的runloop并配置輸入源玩讳。
連接模式:
cocoa使用這個模式與NSConnection結(jié)合來監(jiān)聽回復(fù)涩蜘。你應(yīng)該很少用到這種模式。
語氣模式:
cocoa使用這個模式來區(qū)分模式面板的事件熏纯。
事件追蹤模式:
cocoa使用這種模式來限制在鼠標(biāo)退拽和其他交互追蹤過程中傳入事件同诫。
常用模式:
This is a configurable group of commonly used modes. Associating an input source with this mode also associates it with each of the modes in the group. For Cocoa applications, this set includes the default, modal, and event tracking modes by default. Core Foundation includes just the default mode initially. You can add custom modes to the set using the?CFRunLoopAddCommonMode?function.
這是一個可配置的常用模式組。結(jié)合輸入源和這個模式也就是把它和組中的每個模式結(jié)合樟澜。對于cocoa程序误窖,在默認(rèn)情況下,這包括默認(rèn)模式秩贰,語態(tài)模式以及事件追蹤模式霹俺。核心框架中最初只包含默認(rèn)模式。你可以使用CFRunLoopAddCommonMode毒费,將自定義模式添加到組合中丙唧。
常見的使用:
我們定義輪播時,需要將定時器添加到common模式中觅玻。如果不這樣做的話想际,拖動輪播的時候,由于跟蹤模式會將其他source阻斷溪厘,導(dǎo)致滑動時定時器會不走胡本。添加到common模式中后,不會出現(xiàn)這種問題畸悬。因?yàn)閏ommon包含了跟蹤模式和默認(rèn)模式侧甫,這導(dǎo)致拖動時,定時器還在走不會阻斷。