簡介(摘自官方文檔)
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.
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 objects to 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.
簡單翻譯一下:
運行循環(huán)是與線程相關的基本基礎設施的一部分赊豌,運行循環(huán)是一個用于調度工作并協(xié)調傳入事件的接收的事件處理循環(huán)宁舰。運行循環(huán)作用就是在有工作要做的時候保持線程busy狀態(tài)卢肃,否則讓線程sleep丹莲。
運行循環(huán)管理并非完全自動化。仍然必須手動設計線程的相關代碼姆涩,以便在適當?shù)臅r候啟動運行循環(huán)并響應傳入事件。Cocoa和Core Foundation都提供運行循環(huán)對象以幫助您配置和管理線程的運行。您的應用程序不需要顯式創(chuàng)建這些對象剥槐;每個線程(包括應用程序的主線程)都有相關的運行循環(huán)對象。子級線程需要代碼啟動它們的運行循環(huán)宪摧。應用程序框架自動設置并運行主線程的運行循環(huán)作為應用程序啟動過程的一部分粒竖。ps:謝謝百度翻譯,祝愿越來越牛逼几于。
簡單剖析
運行循環(huán)就像它的名字一樣循環(huán)運行蕊苗,你的線程可以使用它驅動事件處理程序以響應傳入事件。您的代碼提供了用于執(zhí)行循環(huán)的實際循環(huán)部分的控制語句沿彭,換句話說朽砰,您的代碼提供了驅動循環(huán)的while循環(huán)或for循環(huán)。在你的循環(huán)中喉刘,你使用一個運行循環(huán)對象來“運行”事件處理代碼(這個代碼用于接收事件并調用已安裝的處理程序)瞧柔。
運行循環(huán)接收來自兩種不同類型源的事件。輸入源提供異步事件睦裳,通常是來自另一個線程或來自不同應用程序的消息造锅。定時器源提供同步事件,發(fā)生在預定時間或重復間隔廉邑。這兩種類型的源使用特定于應用程序的處理器例程來處理事件到達時备绽。
下圖顯示了運行循環(huán)和各種資源的概念結構:
Run Loop Modes
運行循環(huán)模式是一個input sources (被監(jiān)聽)和 timers sources (被監(jiān)聽)的集合,以及運行循環(huán)觀察員(被通知)的集合鬓催。每次運行運行循環(huán)時肺素,都要指定運行的特定“模式”(無論是顯式的還是隱式的)。在運行循環(huán)的運行的過程中宇驾,只監(jiān)視與該運行模式相關的事件源倍靡,并允許事件源傳遞事件。(類似地课舍,只有與該模式相關聯(lián)的觀察者被通知運行循環(huán)的進度)塌西。與其他模式相關聯(lián)的輸入源保留住任何新事件直到在適當模式。
模式如下:
NSDefaultRunLoopMode: 大多數(shù)操作的運行循環(huán)模式筝尾,很多時候你應該使用這種模式運行runloop捡需。
NSModalPanelRunLoopMode:cocoa使用此模式識別用于模態(tài)面板的事件。
NSEventTrackingRunLoopMode:cocoa在鼠標拖動循環(huán)和其他類型的用戶界面跟蹤循環(huán)中使用此模式限制傳入事件(我們也可以把事件源加入到這種模式)筹淫。
NSRunLoopCommonModes:這是一個復合模式站辉。
小實例
// 1.創(chuàng)建NSTimer
NSTimer *timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(show) userInfo:nil repeats:YES];
// 2.1.添加到當前的runloop中(主運行循環(huán))
// 把定時器加入到默認 NSDefaultRunLoopMode 模式 。
// 拖拽界面,runloop會自動進入UITrackingMode饰剥,此時定時器停止工作殊霞。
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
// 2.2.添加到當前的runloop中(主運行循環(huán))
// 把定時器加入到UITrackingRunLoopMode模式
// 拖拽界面,runloop進入到UITrackingRunLoopMode模式汰蓉,定時器工作绷蹲。
[[NSRunLoop currentRunLoop] addTimer:timer forMode:UITrackingRunLoopMode];
// 2.3.添加到當前的runloop中(主運行循環(huán) )
// 把定時器加入到NSRunLoopCommonModes模式(復合模式)
// 拖拽界面,runloop進入到UITrackingRunLoopMode模式顾孽,定時器工作不受影響祝钢。
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
> 總結:主運行循環(huán)運行在什么mode下運行不是由代碼決定的;當一個運行循環(huán)切換到某種模式時若厚,就可以監(jiān)聽相應模式下的輸入源和通知相應模式下的觀察者了拦英,停止監(jiān)聽和通知切出模式下輸入源和觀察者。
```