RunLoop原理和实例

RunLoop原理

一个run loop就是一个事件处理循环,用来不停的调配工作以及处理输入事件。
Run loop处理的输入事件有两种不同的来源:输入源(input source)和定时源(timer source)。输入源传递异步消息,通常来自于其他线程或者程序。定时源则传递同步消息,在特定时间或者一定的时间间隔发生

Run loop的管理并不完全是自动的。你仍必须设计你的线程代码以在适当的时候启动run loop并正确响应输入事件。Cocoa和CoreFundation都提供了run loop对象方便配置和管理线程的run loop。你创建的程序不需要显示的创建run loop;每个线程,包括程序的主线程(main thread)都有与之相应的run loop对象。但是,自己创建的次线程是需要手动运行run loop的。

RunLoop作用

基本作用
* 应用程序启动后,默认开启一个RunLoop于主线程关联,保持程序的持续运行
* 处理app中各种事件(比如触摸时间,定时器,selector事件)
* 节省cpu资源,提高程序性能:该做事时做事,该休息时休息

RunLoop运行模式

CFRunLoopModeRef代表RunLoop的运行模式
* 一个 RunLoop 包含若干个 Mode,每个Mode又包含若干个Source/Timer/Observer
* 每次RunLoop启动时,只能指定其中一个 Mode,这个Mode被称作 CurrentMode
* RunLoop只能选择一种mode启动, 如果当前Mode中没有任何Source,Timer,Observer。那么直接退出RunLoop
* 如果需要切换Mode,只能退出Loop,再重新指定一个Mode进入

系统默认注册了5个Mode:
kCFRunLoopDefaultMode:App的默认Mode,通常主线程是在这个Mode下运行

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

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

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

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

RunLoop实例

问题:若定时器执行耗时任务,则会导致UI交互卡顿。定时任务触发造成UI卡顿1s

- (void)timeMethod{
    [NSThread sleepForTimeInterval:1.0];
}

1.子线程定时器
* 在子线程启动定时器,并进行定时操作。不阻塞主线程UI。

// 创建一个子线程
- (void)TimerThread{
    _thread = [[NSThread alloc] initWithTarget:self selector:@selector(Timer) object:nil];
    // 启动线程
    [_thread start];
}

- (void)Timer{
    @autoreleasepool {
        //创建定时器并添加到当前线程的RunLoop 模式为Default
        [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(excute) userInfo:nil repeats:YES];
        // 启动当前线程的runloop,若没有则创建
        [[NSRunLoop currentRunLoop] run];
    }
}

// 运行结果:在子线程打印
- (void)excute{
    [NSThread sleepForTimeInterval:1.0];
    NSLog(@"====excute==== %@", [NSThread currentThread]);
}

2.定时器与RunLoop
* 将定时器工作在不同的RunLoopMode下

- (void)RunLoopTimer{

    //等于下面两句的功能,只工作于DefaultRunLoopMode下
    //NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(run) userInfo:nil repeats:YES];

    //定时器只工作于DefaultRunLoopMode下,当用户拖拽的时候定时器不工作
    NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(run) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:timer c];

    //定时器只工作于UITrackingRunLoopMode下,当用户拖拽的时候定时器工作
    NSTimer *timer2 = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(run2) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:timer2 forMode:UITrackingRunLoopMode];

    NSTimer *timer3 = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(run3) userInfo:nil repeats:YES];
    //定时器会运行在标记为CommonModes的模式下
    //标记为CommonModes的模式:UITrackingRunLoopMode和kCFRunLoopDefaultMode
    [[NSRunLoop mainRunLoop] addTimer:timer3 forMode:NSRunLoopCommonModes];
}

- (void)run{
    NSLog(@" NSDefaultRunLoopMode ");
}

- (void)run2{
    NSLog(@" UITrackingRunLoopMode ");
}

- (void)run3{
    NSLog(@" NSRunLoopCommonModes ");
}

运行结果
time1定时器运行在NSDefaultRunLoopMode模式下,即没有进行scrollView拖拽。
time2定时器运行在UITrackingRunLoopMode模式下,即进行scrollView拖拽
time3定时器运行于NSRunLoopCommonModes模式下,无论scrollView是否发生拖拽行为都会执行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值