NSRunLoop(來自官方文檔)

來源:互聯網
上載者:User

標籤:

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.

Your application cannot either create or explicitly manage NSRunLoop objects. Each NSThreadobject, 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.

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通常不被認為是安全執行緒的,並且只應該在當前線程的上下文環境中調用它的方法。不要嘗試在另一個不同的線程中調用NSRunLoop對象的方法,這樣做可能造成未知的錯誤。

Timer和NSRunLoop結合工作,但他們不能提供一個真正的時間機制,準確性受限。如果你只是想在未來的某個時間點發送一個訊息,可以不用timer,Timer就是NSTimer對象。

Timers are represented by NSTimer objects. They work in conjunction with NSRunLoopobjects. NSRunLoop objects control loops that wait for input, and they use timers to help determine the maximum amount of time they should wait. When the timer’s time limit has elapsed, the run loop fires the timer (causing its message to be sent), then checks for new input.

The run loop mode in which you register the timer must be running for the timer to fire. For applications built using the Application Kit or UIKit, the application object runs the main thread’s run loop for you. On secondary threads, however, you have to run the run loop yourself.

RunLoop是和線程相關的基礎設施。一個runloop是一個時間處理loop,你可以安排工作、協調the receipt of incoming events。runloop的目的是有工作的時候,讓線程忙,沒有工作,就讓線程sleep。

Run loop管理不是完全自動的。你必須設計你的線程代碼,在合適的時間啟動run loop,並相應到來的事件。Cocoa和Core Foundation提供runloop對象協助你組態管理你的線程的runloop。你的應用程式沒有必要顯式的建立這些對象;每個線程(包括應用程式主線程)有一個關聯的runloop對象。只有secondar thread需要顯式運行他們的runloop。app架構(frameworks)在主線程自動啟動並運行runloop,作為程式啟動進程的一部分。

Anatomy of a Run Loop

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.

run loop從兩種不同類型的源接收事件。Input sources deliver非同步事件,通常是從另一個線程或一個不同應用的訊息。Timer source deliver ,不多解釋了。這兩種源到達時,都使用應用程式特定處理路徑。

下面的圖3-1顯示了run loop概念上的結構和各種源(source)。Input source deliver非同步事件給合適的處理者,並造成 runUntilDate: 方法(被和線程相關的runloop對象調用)退出。Timer source deliver events to their handler routines(理解,翻譯不通,有個懂英語的妹子多好),但不會造成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組件和模式的資訊,也描述了在處理事件的不同時間,產生的通知。

Run Loop Modes

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 you run loop, you specify(顯式或隱式指定都行)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.(關鍵的東西不敢亂翻譯啊,膽小)

在你的代碼裡,通過名字來區別不同的模式(modes)。Cocoa和Core Foundation定義一個預設的和幾個常用的模式。你可以自訂模式,通過為模式的名字指定一個字串。模式的名字可以隨便起,但內容不能。你要確保一個或者更多input sources、timers或者run-loop觀察者被加到你建立的模式中。

You use modes to filter out events from unwanted sources during a particular pass through your run loop. Most of time, you will want to run you run loop in the system-defined "default" mode. A modal panel, however, might run in the "modal" mode. While in this mode, only sources revevant to the model 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.

注意: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.

 

消化消化,吃飯去。有時間繼續

NSRunLoop(來自官方文檔)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.