RunLoop的各种运行模式

RunLoop运行模式相关

Core Foundation框架中关于RunLoop的5个类

CFRunLoopRef

CFRunLoopModeRef

CFRunLoopSourceRef

CFRunLoopTimerRef 基于时间的触发器,基本上说的就是NSTimer

CFRunLoopObserverRef`

在RunLoop中有多个运行模式,但RunLoop只能选择一种模式运行

Mode里面至少要有一个Timer或Source

CFRunLoopModeRef代表RunLoop的运行模式

一个RunLoop包含若干个Mode,每个Mode又包含若干个Source/Timer/Observer

每次RunLoop启动时,只能指定其中一个Mode,这个Mode被称作CurrentMode

如果需要切换Mode,只能退出Loop ,在重新指定一个Mode进入

这样做主要是为了分割开不同组的Source/Timer/Observer,让其互不影响

系统默认注册了5个Mode常用的有3个

kCFRynLoopDefaultMode:App的默认Mode,通常主线程是在这个Mode下运行

UITrackingRunLoopMode:界面跟踪Mode,用于ScrollView追踪触摸滑动,保证界面滑动时不受其他Mode影响

kCFRunLoopCommonModes:这是一个占位用的Mode,不是一种真正的Mode

UIInitializationRunLoopMode:在刚启动App时进入的第一个Mode,启动完成后不再使用

GSEventReceiveRunLoopMode:接受系统事件的内部Mode,通常用不到

代码示例

主线程已经自动创建了Runloop

下述指定Runloop的运行模式为 NSDefaultRunLoopMode

缺点:在UIScrollView拖拽的时候定时器会等到拖拽结束才继续执行

原因:UIScrollView滚动时Timer失效

UIScrollView滚动时,RunLoop模型模式切换到了UITrackingRunLoopMode模式,所以导致Timer失效

UITrackingRunLoopMode:界面追踪运行模式,定时器只有在scrollview拖拽的时候才会运行

@implementationViewController- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event{    [selftimer];}- (void)timer {// 1. 创建定时器 : 使用该方法创建的定时器是不会添加到RunLoop中的NSTimer*timer = [NSTimertimerWithTimeInterval:2.0target:selfselector:@selector(run) userInfo:nilrepeats:YES];// 2. 添加定时器到runloop中,指定runloop的运行模式为默认// 2.1 定时器// 2.2 runloop的运行模式[[NSRunLoopcurrentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode]; }- (void)run {NSLog(@"run -------%@---%@",[NSThreadcurrentThread],[NSRunLoopcurrentRunLoop].currentMode);}@end

下述指定Runloop的运行模式为 UITrackingRunLoopMode

缺点:正常情况下不会执行定时器,拖拽UIScrollview的时候才会执行定时器任务

@implementationViewController- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event{    [selftimer];}- (void)timer{// 1. 创建定时器 : 使用该方法创建的定时器是不会添加到RunLoop中的NSTimer*timer = [NSTimertimerWithTimeInterval:2.0target:selfselector:@selector(run) userInfo:nilrepeats:YES];// 指定Timer即可在scrollView滑动时出发定时器,但是这样在scrollview不滑动的情况下Timer就不会触发[[NSRunLoopcurrentRunLoop] addTimer:timer forMode:UITrackingRunLoopMode];}- (void)run {NSLog(@"run -------%@---%@",[NSThreadcurrentThread],[NSRunLoopcurrentRunLoop].currentMode);}@end

下述指定Runloop的运行模式为 NSRunLoopCommonModes

缺点:完美,解决上述两种运行模式的缺陷

NSRunLoopCommonModes = NSDefaultRunLoopMode && UITrackingRunLoopMode

占用,标签,凡是添加到NSRunLoopCommonModes中的事件都会被同时添加到打上common标签的运行模式下

输出运行模式结果

entries =>0: {contents ="UITrackingRunLoopMode"}2: {contents ="kCFRunLoopDefaultMode"}

@implementationViewController- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event{    [selftimer];}- (void)timer{// 1. 创建定时器 : 使用该方法创建的定时器是不会添加到RunLoop中的NSTimer*timer = [NSTimertimerWithTimeInterval:2.0target:selfselector:@selector(run) userInfo:nilrepeats:YES];//解决方法一:添加两种模式:比较笨的方法//解决方法二:[[NSRunLoopcurrentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];//    NSLog(@"%@",[NSRunLoop currentRunLoop]);输出可查看被打上标签的模式}- (void)run {NSLog(@"run -------%@---%@",[NSThreadcurrentThread],[NSRunLoopcurrentRunLoop].currentMode);}@end

子线程中需要手动创建之后开启runloop才能保证线程不死,定时器才能正常工作

#import"ViewController.h"@implementationViewController- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event{// 如果这个方法在子线程中调用[NSThreaddetachNewThreadSelector:@selector(timer1) toTarget:selfwithObject:nil];}- (void)timer{NSRunLoop*currentRunLoop = [NSRunLoopcurrentRunLoop];// 1. 创建定时器 : 该方法内部会自动添加到runloop中,并且设置运行模式为默认,但在子线程中默认不会创建RunLoop,需手动创建[NSTimerscheduledTimerWithTimeInterval:2.0target:selfselector:@selector(run) userInfo:nilrepeats:YES];// 子线程创建runloop后默认不开启// 开启runloop[currentRunLoop run];}- (void)run {NSLog(@"run -------%@---%@",[NSThreadcurrentThread],[NSRunLoopcurrentRunLoop].currentMode);}@end

你可能感兴趣的:(RunLoop的各种运行模式)