一、RunLoop基本概念
RunLoop從字面意思上看:
運(yùn)行循環(huán)
跑圈
RunLoop的基本作用:
保持程序的持續(xù)運(yùn)行
處理APP中各種事件(比如:觸摸事件,定時(shí)器事件,Selector事件等)
能節(jié)省CPU資源,提高程序的性能:該做事的時(shí)候就喚醒,沒(méi)有事情就睡眠
假如沒(méi)有了RunLoop:
大家都知道程序的入口是main函數(shù):
int main(int argc, char * argv[]) {
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
如果沒(méi)有RunLoop,程序就會(huì)在main函數(shù)執(zhí)行完畢的時(shí)候退出,也正是因?yàn)橛辛薘unLoop,導(dǎo)致main函數(shù)沒(méi)有馬上退出,保證了程序持續(xù)運(yùn)行;
其實(shí)是在UIApplicationMain函數(shù)內(nèi)部啟動(dòng)了一個(gè)RunLoop;
這個(gè)默認(rèn)啟動(dòng)的RunLoop是跟主線程相關(guān)聯(lián)的
RunLoop內(nèi)部其實(shí)是有一個(gè)do-while循環(huán)(可以從RunLoop源碼中找到),暫且可以理解為下面的代碼:
二、RunLoop對(duì)象
iOS中有2套API來(lái)訪問(wèn)和使用RunLoop
Foundation框架中的NSRunLoop;
Core Foundation中的CFRunLoop;
NSRunLoop是基于CFRunLoopRef的一層OC包裝,所以要了解RunLoop內(nèi)部結(jié)構(gòu),需要多研究CFRunLoopRef層面的API(Core Foundation層面)
三、RunLoop與線程
每條線程都有唯一的一個(gè)與之對(duì)應(yīng)的RunLoop對(duì)象
主線程中的RunLoop由系統(tǒng)自動(dòng)創(chuàng)建,子線程中RunLoop可以通過(guò)手動(dòng)創(chuàng)建
RunLoop在線程結(jié)束的時(shí)候會(huì)被銷毀
獲取RunLoop對(duì)象
Foundation框架中
[NSRunLoop currentRunLoop]; // 獲得當(dāng)前線程的RunLoop對(duì)象
[NSRunLoop mainRunLoop]; // 獲得主線程的RunLoop對(duì)象
Core Foundation框架中
CFRunLoopGetCurrent(); // 獲得當(dāng)前線程的RunLoop對(duì)象
CFRunLoopGetMain(); // 獲得主線程的RunLoop對(duì)象
四、RunLoop的結(jié)構(gòu)(為更深入的了解RunLoop我們研究CFRunLoop)
- CFRunLoopModeRef:
CFRunLoopModeRef代表RunLoop的運(yùn)行模式
一個(gè)RunLoop中包含N多個(gè)Mode,每個(gè)Mode中又包含了N多個(gè)Source/Timer/Observer
一個(gè)RunLoop在同一時(shí)間內(nèi)只能處在一種運(yùn)行模式下,這個(gè)模式也就是CurrentMode
一個(gè)RunLoop的運(yùn)行模式可以切換,是在當(dāng)前Mode退出后,下次進(jìn)入的時(shí)候切換
系統(tǒng)默認(rèn)注冊(cè)了5中Mode:
NSDefaultRunLoopMode:默認(rèn)的Mode,通常主線程的RunLoop是在這個(gè)Mode下運(yùn)行
UITrackingRunLoopMode:界面跟蹤Mode,當(dāng)用戶與界面交互的時(shí)候會(huì)在此Mode下運(yùn)行
NSRunLoopCommonModes:這個(gè)不是一種真正的Mode,是一個(gè)占位用的Mode
UIInitializationRunLoopMode:程序啟動(dòng)時(shí)的Mode,啟動(dòng)完成后就不在此Mode下
GSEventReceiveRunLoopMode:接受系統(tǒng)事件的內(nèi)部Mode,一般我們用不到 - CFRunLoopTimerRef
CFRunLoopTimerRef是基于時(shí)間的觸發(fā)器
CFRunLoopTimerRef基本上說(shuō)的就是NSTimer,它受RunLoop Mode的影響
// 創(chuàng)建一個(gè)定時(shí)器
NSTimer *timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(run) userInfo:nil repeats:YES];
// NSDefaultRunLoopMode:NSTimer只有在默認(rèn)模式下(NSDefaultRunLoopMode)工作,切換到其他模式不再工作,比如拖拽了界面上的某個(gè)控件(會(huì)切換成UITrackingRunLoopMode)
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
// 創(chuàng)建一個(gè)定時(shí)器
NSTimer *timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(run) userInfo:nil repeats:YES];
// 拖拽UI界面時(shí)出發(fā)定時(shí)器,在默認(rèn)模式(NSDefaultRunLoopMode)下不工作
[[NSRunLoop mainRunLoop] addTimer:timer forMode:UITrackingRunLoopMode];
// 創(chuàng)建一個(gè)定時(shí)器
NSTimer *timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(run) userInfo:nil repeats:YES];
// NSRunLoopCommonModes僅僅是標(biāo)記NSTimer在兩種模式(UITrackingRunLoopMode/NSDefaultRunLoopMode)下都能運(yùn)行,但一個(gè)RunLoop中同一時(shí)間內(nèi)只能運(yùn)行一種模式
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
// 默認(rèn)已經(jīng)添加到主線程中RunLoop(mainRunLoop)中(Mode:NSDefaultRunLoopMode)
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(run) userInfo:nil repeats:YES];
GCD定時(shí)器不受RunLoop Mode的影響
/** 定時(shí)器對(duì)象 */
@property (nonatomic, strong)dispatch_source_t timer;
// 需要一個(gè)強(qiáng)引用
NSLog(@"開(kāi)始");
// 獲取隊(duì)并發(fā)隊(duì)列,定時(shí)器的回調(diào)函數(shù)將會(huì)在子線程中執(zhí)行
// dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
// 獲取主隊(duì)列,定時(shí)器的回調(diào)函數(shù)將會(huì)在子線程中執(zhí)行
dispatch_queue_t queue = dispatch_get_main_queue();
self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
// 該時(shí)間表示從現(xiàn)在開(kāi)始推遲兩秒
dispatch_time_t start = dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC);
// 設(shè)置定時(shí)器的開(kāi)始時(shí)間,間隔時(shí)間
dispatch_source_set_timer(self.timer, start, 1 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
dispatch_source_set_event_handler(self.timer, ^{
NSLog(@"------%@", [NSThread currentThread]);
});
dispatch_resume(self.timer);
/* 參數(shù)說(shuō)明:
// 設(shè)置定時(shí)器的一些屬性
dispatch_source_set_timer(dispatch_source_t source, // 定時(shí)器對(duì)象
dispatch_time_t start, // 定時(shí)器開(kāi)始執(zhí)行的時(shí)間
uint64_t interval, // 定時(shí)器的間隔時(shí)間
uint64_t leeway // 定時(shí)器的精度
);
*/
- CFRunLoopSourceRef
按照官方文檔CFRunLoopSourceRef為3類
Port-Based Sources:與內(nèi)核相關(guān)
Custom Input Sources:與自定義Sources相關(guān)
Cocoa Perform Selector Sources:與Performxxxxxx等方法等相關(guān)
按照函數(shù)調(diào)用棧CFRunLoopSourceRef分2類:
Source0:非基于Port的
Source1:基于Port的,通過(guò)內(nèi)核和其他線程通信,接收、分發(fā)系統(tǒng)事件 - CFRunLoopObserverRef
CFRunLoopObserverRef是RunLoop的觀察者,可以通過(guò)CFRunLoopObserverRef來(lái)監(jiān)聽(tīng)RunLoop狀態(tài)的改變
CFRunLoopObserverRef監(jiān)聽(tīng)的狀態(tài)由以下幾種:
/* Run Loop Observer Activities */
typedef CF_OPTIONS(CFOptionFlags, CFRunLoopActivity) {
kCFRunLoopEntry = (1UL << 0), // 狀態(tài)值:1,表示進(jìn)入RunLoop
kCFRunLoopBeforeTimers = (1UL << 1), // 狀態(tài)值:2,表示即將處理NSTimer
kCFRunLoopBeforeSources = (1UL << 2), // 狀態(tài)值:4,表示即將處理Sources
kCFRunLoopBeforeWaiting = (1UL << 5), // 狀態(tài)值:32,表示即將休眠
kCFRunLoopAfterWaiting = (1UL << 6), // 狀態(tài)值:64,表示從休眠中喚醒
kCFRunLoopExit = (1UL << 7), // 狀態(tài)值:128,表示退出RunLoop
kCFRunLoopAllActivities = 0x0FFFFFFFU // 表示監(jiān)聽(tīng)上面所有的狀態(tài)
};
```
如何監(jiān)聽(tīng)RunLoop的狀態(tài):
1.創(chuàng)建CFRunLoopObserverRef
// 第一個(gè)參數(shù)用于分配該observer對(duì)象的內(nèi)存
// 第二個(gè)參數(shù)用以設(shè)置該observer所要關(guān)注的的事件,詳見(jiàn)回調(diào)函數(shù)myRunLoopObserver中注釋
// 第三個(gè)參數(shù)用于標(biāo)識(shí)該observer是在第一次進(jìn)入run loop時(shí)執(zhí)行還是每次進(jìn)入run loop處理時(shí)均執(zhí)行
// 第四個(gè)參數(shù)用于設(shè)置該observer的優(yōu)先級(jí),一般為0
// 第五個(gè)參數(shù)用于設(shè)置該observer的回調(diào)函數(shù)
// 第六個(gè)參數(shù)observer的運(yùn)行狀態(tài)
CFRunLoopObserverRef observer = CFRunLoopObserverCreateWithHandler(CFAllocatorGetDefault(), kCFRunLoopAllActivities, YES, 0, ^(CFRunLoopObserverRef observer, CFRunLoopActivity activity) {
NSLog(@"----監(jiān)聽(tīng)到RunLoop狀態(tài)發(fā)生改變---%zd", activity);
});
2.將觀察者CFRunLoopObserverRef添加到RunLoop上面
CFRunLoopAddObserver(CFRunLoopGetCurrent(), observer, kCFRunLoopDefaultMode);
3.觀察者CFRunLoopObserverRef要手動(dòng)釋放
CFRelease(observer);
五、RunLoop的處理邏輯

上圖顯示了線程的輸入源
1.基于端口的輸入源(Port Sources)
2.自定義輸入源(Custom Sources)
3.Cocoa執(zhí)行Selector的源(performSelectorxxxx方法)
4.定時(shí)源(Timer Sources )
線程針對(duì)上面不同的輸入源,有不同的處理機(jī)制
1.handlePort——處理基于端口的輸入源
2.customSrc——處理用戶自定義輸入源
3.mySelector——處理Selector的源
4.timerFired——處理定時(shí)源
非官方文檔

六、RunLoop的具體使用
<1>.圖片刷新(假如界面要刷新N多圖片(渲染),此時(shí)用戶拖拽UI控件就會(huì)出現(xiàn)卡的效果,我們可以通過(guò)RunLoop實(shí)現(xiàn),只在RunLoop默認(rèn)Mode下下載,也就是拖拽Mode下不刷新圖片)
- (void)viewDidLoad {
[super viewDidLoad];
// 只在NSDefaultRunLoopMode下執(zhí)行(刷新圖片)
[self.myImageView performSelector:@selector(setImage:) withObject:[UIImage imageNamed:@"0"] afterDelay:2.0 inModes:@[NSDefaultRunLoopMode]];
}
<2>TableView中實(shí)現(xiàn)平滑滾動(dòng)延遲加載圖片
UIImage *downloadedImage = ...;
[self.avatarImageView performSelector:@selector(setImage:)
withObject:downloadedImage
afterDelay:0
inModes:@[NSDefaultRunLoopMode]];
<3>:(子線程中加入RunLoop+RunLoop源)建議采用此方案
備注:RunLoop源:Port、Sources0/Sources1、Timer
import "ViewController.h"
/*
思路:為了保證線程不死,我們考慮在子線程中加入RunLoop,
但是由于RunLoop中沒(méi)有沒(méi)有源,就會(huì)自動(dòng)退出RunLoop,
所以我們要為子線程添加一個(gè)RunLoop,
并且為這個(gè)RunLoop添加源(保證RunLoop不退出)
*/
@interface ViewController ()
/** 線程對(duì)象 */
@property (nonatomic, strong)NSThread *thread;
@end
@implementation ViewController
-
(void)viewDidLoad {
[super viewDidLoad];// 創(chuàng)建子線程
self.thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];//啟動(dòng)子線程
[self.thread start];
}
-
(void)run {
NSLog(@"run--%@", [NSThread currentThread]); // 子線程
// 給子線程添加一個(gè)RunLoop,并且加入源
[[NSRunLoop currentRunLoop] addPort:[NSPort port] forMode:NSDefaultRunLoopMode];
// 啟動(dòng)RunLoop
[[NSRunLoop currentRunLoop] run];NSLog(@"------------"); // RunLoop啟動(dòng),這句沒(méi)有執(zhí)行的機(jī)會(huì)
} -
(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// 在子線程中調(diào)用test方法,如果子線程還在就能夠調(diào)用成功
[self performSelector:@selector(test) onThread:self.thread withObject:nil waitUntilDone:YES modes:@[NSDefaultRunLoopMode]];
} (void)test {
NSLog(@"test--%@", [NSThread currentThread]); // 子線程
}
@end
主線程之所以沒(méi)有退出,就是因?yàn)橹骶€程內(nèi)部自動(dòng)開(kāi)啟了一個(gè)RunLoop.
<4> 利用對(duì)線程的強(qiáng)引用,保證子線程永遠(yuǎn)存活是不可以的,線程是執(zhí)行完runing方法后就會(huì)死亡,即使內(nèi)存還在,但是該線程也已經(jīng)處于死亡狀態(tài)(線程狀態(tài)),是不能再次啟動(dòng)的,
如果再次啟動(dòng)一個(gè)死亡狀態(tài)的線程,就會(huì)
報(bào)錯(cuò)--reason: '*** -[YCThread start]: attempt(視圖) to start the thread again',所以setImage:方法也就不會(huì)被執(zhí)行
/* 利用對(duì)線程的強(qiáng)引用,保證子線程永遠(yuǎn)存活是不可以的,線程是執(zhí)行完runing方法后就會(huì)死亡,即使內(nèi)存還在,但是該線程也已經(jīng)處于死亡狀態(tài)(線程狀態(tài)),是不能再次啟動(dòng)的,
如果再次啟動(dòng)一個(gè)死亡狀態(tài)的線程,就會(huì)
報(bào)錯(cuò)--reason: '*** -[YCThread start]: attempt(視圖) to start the thread again',所以setImage:方法也就不會(huì)被執(zhí)行
*/
- (void)Runloop4{
// 創(chuàng)建子線程
self.thread = [[NSThread alloc] initWithTarget:self selector:@selector(runing) object:nil];
NSLog(@"當(dāng)前線程為%@",self.thread);
[NSThread sleepForTimeInterval:2.0];
//啟動(dòng)子線程
[self.thread start];
}
-
(void)runing{
[self.IMAGE performSelector:@selector(setImage:) onThread:self.thread withObject:[UIImage imageNamed:@"0"] waitUntilDone:NO modes:@[NSDefaultRunLoopMode]];
// 此處報(bào)錯(cuò)正好說(shuō)明self.thread線層已經(jīng)被回收了,不存在了
[self.thread start];
}
- (void)setImage:(UIImage *)image{
NSLog(@"----當(dāng)前隊(duì)列為-----%@", [NSThread currentThread]);
[self setImage:image];
}
[本文引自]http://www.lxweimin.com/p/4bc01f5269e7