Runloop小结

作用:

Runloop的本质是线程的“经纪人”,专门给线程找事干。

是一个死循环,保持线程活着,有活干活,没活休眠。而不会让一个线程一个任务执行完了,马上释放掉。

Runloop里面维护了一个消息队列,用于存放线程要干的活。活分为很多种,也就是下面将要提到的事件源。

基于事件驱动的应用,都会有一个类似的runloop机制,这个区别于基于命令的程序。这套机制与平台无关,与业务机制相关。

创建:

主线程的runloop由系统创建,子线程的runloop的创建方式如下(参考AFNetworking)。

NSRunLoop*runLoop = [NSRunLoop currentRunLoop];

[runLoop addPort:[NSMachPortport]forMode:NSDefaultRunLoopMode];

[runLoop run];

Runloop能够接受的事件源:

1,NSTimer事件。CADisplayLink事件,类似timer的一种机制,屏幕每刷新一帧,触发一次执行。

2,UIEvent:iOS支持三种事件,触摸事件,运动事件,远程控制事件,通过source0触发。

3,NSObject(NSDelayedPerforming)

----NSRunLoop.h

@interfaceNSObject (NSDelayedPerforming) 

- (void)performSelector:(SEL)aSelector withObject:(nullableid)anArgument afterDelay:(NSTimeInterval)delay inModes:(NSArray *)modes;

- (void)performSelector:(SEL)aSelector withObject:(nullableid)anArgument afterDelay:(NSTimeInterval)delay;

+ (void)cancelPreviousPerformRequestsWithTarget:(id)aTarget selector:(SEL)aSelector object:(nullableid)anArgument;

+ (void)cancelPreviousPerformRequestsWithTarget:(id)aTarget;

@end

4,NSObject(NSThreadPerformAddition)

---NSThread.h

@interfaceNSObject (NSThreadPerformAdditions)

- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(nullableid)arg waitUntilDone:(BOOL)wait modes:(nullableNSArray *)array;

- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(nullableid)arg waitUntilDone:(BOOL)wait;

// equivalent to the first method with kCFRunLoopCommonModes

- (void)performSelector:(SEL)aSelector onThread:(NSThread*)thr withObject:(nullableid)arg waitUntilDone:(BOOL)wait modes:(nullableNSArray *)arrayNS_AVAILABLE(10_5,2_0);

- (void)performSelector:(SEL)aSelector onThread:(NSThread*)thr withObject:(nullableid)arg waitUntilDone:(BOOL)waitNS_AVAILABLE(10_5,2_0);

// equivalent to the first method with kCFRunLoopCommonModes

- (void)performSelectorInBackground:(SEL)aSelector withObject:(nullableid)argNS_AVAILABLE(10_5,2_0);

@end

5,NSUrlConnection。回调事件在主线程的RunLoop中。

6,其他:autorelease对象释放,动画(CATransition,CAAnimation),NSPort,dispatch_get_main_queue()。

与runloop相关的类

Runloop起作用举例,如点击事件。

runloop调用堆栈


runloop处理事件的方法
runloop下面可以有几个mode,每个mode又可以有多个源
Source源

CFRunLoopSource:

Source0,app内部事件,如触摸事件等等

Source1,进程通信事件。

NSTimer----CFRunLoopTimer;


runloop的状态


autorelease对象释放


runloop mode特点



mode种类



scrollview滚动时runloop的切换



GCD与runloop的关系

GCD也有延迟执行的接口,这个延迟执行,据说跟Timer的机制不一样。只有GCD中在mainQueue中block的执行与RunLoop有关系。

performSelector是基于runLoop的,因此如果在子线程中,子线程没有开启runLoop,则调用方法不会生效。

GCD的延迟执行的函数,不是基于RunLoop的,因此不担心子线程调用的问题。

GCD里面延迟操作的逻辑,并不是基于Timer的。

Core Fundation部分开源的代码。

http://opensource.apple.com/source/CF/CF-855.14/

http://opensource.apple.com/source/CF/CF-855.17/

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、什么是runloop 字面意思是“消息循环、运行循环”。它不是线程,但它和线程息息相关。一般来讲,一个线程一次...
    WeiHing阅读 8,177评论 11 111
  • ======================= 前言 RunLoop 是 iOS 和 OSX 开发中非常基础的一个...
    i憬铭阅读 912评论 0 4
  • 开启线程 分离主线程创建:创建线程后会自动执行,但是线程外部不可获取到该线程对象detachNewThreadWi...
    Mr_Pt阅读 1,098评论 0 1
  • 假装水彩的国画颜料 唉,我敷衍绘画,绘画也敷衍我了。 浪费了许多时间,笔触手感很不好。 一个人的时间用在哪里真的是...
    巫落阅读 181评论 0 1
  • (1)工资与回家 空先生月中收到了人生第一笔工资,开心得不得了。 开心完他严肃跟我说,“把我的工资卡跟你的支付宝绑...
    梨子的白日梦阅读 266评论 0 0