1.Runloop是什么?
Runloop是线程的基础架构部分.是一个事件循环处理.是用来不停的调配工作(可以节省CPU)和处理输入事件(输入源(input source)和定时源(timer source))(后面解释).
2.Runloop在哪?
每一个线程,包括主线程,都对应一个Runloop,主线程的Runloop默认是运行的,自己创建的需要手动运行
3.什么时候用Runloop
当需要和改线程进行交互的时候,并且需要改线程监听某项事务的时候,主线程Runloop默认是开启的,如果是自己创建的线程,需要手动运行Runloop,来让线程一直不退出,Runloop就是有事件的时候执行对应的函数,没有的时候进行休眠
4.Runloop处理的输入事件:输入源(input source)和定时源(timer source)
4.1 输入源(input source):
传递异步事件,通常消息来自于其他线程或程序。输入源传递异步消息给相应的处理例程,并调用runUntilDate:方法来退出(在线程里面相关的NSRunLoop对象调用)
4.2 定时源(timer source)
定时源在预设的时间点同步方式传递消息,这些消息都会发生在特定时间或者重复的时间间隔。定时源则直接传递消息给处理例程,不会立即退出run loop
// 创建定时器源有两种方法, //方法一: NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:4.0 target:self selector:@selector(backgroundThreadFire:) userInfo:nil repeats:YES]; [[NSRunLoop currentRunLoop] addTimer:timerforMode:NSDefaultRunLoopMode]; //方法二: [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(backgroundThreadFire:) userInfo:nil repeats:YES];
5.Runloop的观察者
run loop观察者则是在run loop本身运行的特定时候触发,在创建的时候你可以指定run loop观察者可以只用一次或循环使用。若只用一次,那么在它启动后,会把它自己从run loop里面移除,而循环的观察者则不会。定义观察者并把它添加到run loop,只能使用Core Fundation
//如何创建run loop的观察者: - (void)addObserverToCurrentRunloop { // The application uses garbage collection, so noautorelease pool is needed. NSRunLoop*myRunLoop = [NSRunLoop currentRunLoop]; // Create a run loop observer and attach it to the runloop. CFRunLoopObserverContext context = {0, self, NULL, NULL, NULL}; CFRunLoopObserverRef observer =CFRunLoopObserverCreate(kCFAllocatorDefault, kCFRunLoopBeforeTimers, YES, 0, &myRunLoopObserver, &context); if (observer) { CFRunLoopRef cfLoop = [myRunLoop getCFRunLoop]; CFRunLoopAddObserver(cfLoop, observer, kCFRunLoopDefaultMode); } } //其中,kCFRunLoopBeforeTimers表示选择监听定时器触发前处理事件,后面的YES表示循环监听。
6.RunLoop的事件队列
每次运行run loop,你线程的run loop对会自动处理之前未处理的消息,并通知相关的观察者。具体的顺序如下:
参考:http://blog.csdn.net/ztp800201/article/details/9240913
原文:http://www.cnblogs.com/miaomiaocat/p/5084110.html