欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS学习笔记-137.RunLoop05——Runloop相关类3_CFRunLoopTimerRef(NSTimer)为何定时有时会失败

程序员文章站 2022-05-27 08:31:43
ios学习笔记-137.runloop05——runloop相关类3_cfrunlooptimerref(nstimer)为何定时有时会失败。 一、cfrunlooptim...

ios学习笔记-137.runloop05——runloop相关类3_cfrunlooptimerref(nstimer)为何定时有时会失败。

iOS学习笔记-137.RunLoop05——Runloop相关类3_CFRunLoopTimerRef(NSTimer)为何定时有时会失败

一、cfrunlooptimerref 主要说明

cfrunlooptimerref是基于时间的触发器

基本上说的就是nstimer,它会受到runloop的mode的影响

gcd的定时器不受runloop的mode的影响


二、nstimer定时器不能用了问题演示

2.1 nstimer定时器不能用了,图示

iOS学习笔记-137.RunLoop05——Runloop相关类3_CFRunLoopTimerRef(NSTimer)为何定时有时会失败

2.2 nstimer定时器不能用了代码示例1

-(void)timer1{
    //该方法内部自动添加到runloop中,并且设置运行模式为默认
    nstimer *timer = [nstimer scheduledtimerwithtimeinterval:1.0 target:self selector:@selector(run) userinfo:nil repeats:yes];
}

2.3 nstimer定时器不能用了代码示例2

我们首先在主线程上创建一个定时器,并且把它添加到默认模式下。

-(void)timer2{
    //创建定时器
    nstimer *timer = [nstimer timerwithtimeinterval:1.0 target:self selector:@selector(run) userinfo:nil repeats:yes];

    //2.添加定时器到runloop中,指定runloop的运行模式为nsdefaultrunloopmode
     /*
        第一个参数:定时器
        第二个参数:runloop的运行模式
       */
    [[nsrunloop mainrunloop] addtimer:timer formode:nsdefaultrunloopmode];
}

三、nstimer定时器不能用了解决方式

3.1 问题分析与解决方式说明

由前面的学习,我们知道,当我们的 uitextview 滚动的时候,我们主线程的运行模式是 uitrackingrunloopmode。然而我们的定时器是添加在 nsdefaultrunloopmode,模式下。它能再 uitrackingrunloopmode下运行,那不就见鬼了吗?

说了这么多?那么我们的解决方式也就是来了,我们把定时器在添加到 uitrackingrunloopmode 模式下,不就ok了吗?。除此之外,我们还可以把它添加到 nsrunloopcommonmodes模式下,因为 nsrunloopcommonmodes = nsdefaultrunloopmode + uitrackingrunloopmode


3.2 针对 2.2 中的解决方式1

-(void)timer1{
    //该方法内部自动添加到runloop中,并且设置运行模式为默认
    nstimer *timer = [nstimer scheduledtimerwithtimeinterval:1.0 target:self selector:@selector(run) userinfo:nil repeats:yes];
    //可以通过如下来修改它的运行模式
    [[nsrunloop currentrunloop] addtimer:timer formode:uitrackingrunloopmode];
}

3.3 针对 2.2 中的解决方式2

推荐这种

-(void)timer1{
    //该方法内部自动添加到runloop中,并且设置运行模式为默认
    nstimer *timer = [nstimer scheduledtimerwithtimeinterval:1.0 target:self selector:@selector(run) userinfo:nil repeats:yes];
    //可以通过如下来修改它的运行模式
    [[nsrunloop currentrunloop] addtimer:timer formode:nsrunloopcommonmodes];
}

3.4 针对 2.3 中的解决方式1

-(void)timer2{
    //创建定时器
    nstimer *timer = [nstimer timerwithtimeinterval:1.0 target:self selector:@selector(run) userinfo:nil repeats:yes];

    //2.添加定时器到runloop中,指定runloop的运行模式为nsdefaultrunloopmode
     /*
        第一个参数:定时器
        第二个参数:runloop的运行模式
       */
    [[nsrunloop mainrunloop] addtimer:timer formode:nsdefaultrunloopmode];

    // 3.1 第一种方式,再把它添加到 uitrackingrunloopmode
    [[nsrunloop mainrunloop] addtimer:timer formode:uitrackingrunloopmode];
}

3.5 针对 2.3 中的解决方式2

推荐这种

-(void)timer2{
    //创建定时器
    nstimer *timer = [nstimer timerwithtimeinterval:1.0 target:self selector:@selector(run) userinfo:nil repeats:yes];

    //nsrunloopcommonmodes = nsdefaultrunloopmode + uitrackingrunloopmode
    //占用,标签,凡是添加到nsrunloopcommonmodes中的事件 都会被同时添加到打上commmon标签的运行模式上
    [[nsrunloop mainrunloop] addtimer:timer formode:nsrunloopcommonmodes];
}

四、子线程中使用 nstimer 定时器

4.1 子线程中不能运行 nstimer 定时器?

我们创建了如下代码,

-(void)viewdidload{
    [nsthread detachnewthreadselector:@selector(timer3) totarget:self withobject:nil];
}

-(void)timer3{ 
    //创建一个定时器
    //该方法内部自动添加到runloop中,并且设置运行模式为默认
    [nstimer scheduledtimerwithtimeinterval:1.0 target:self selector:@selector(run) userinfo:nil repeats:yes];
}

我们发现,我们的定时器,不能工作了?那么怎么解决呢?

4.2 问题分析月解决方式说明

分析上面的问题,我们知道,nstimer是需要 runloop 的。之前我们在主线程中运行创建nstimer的时候,不需要自己手动来获取runloop那是因为,主线程中的 runloop 已经自动创建了。然而子线程中的 runloop 是需要我们手动创建的。而创建的方法就是调用 currentrunloop 方法,如实我们就有了解决方案,就是自己创建 runloop 并且运行它。

4.3 解决示例代码

于是针对上面的代码,我们就有了下面的解决代码

-(void)timer3{
    //子线程中,默认是没有创建 runloop 的,可以通过 [nsrunloop currentrunloop] 来创建,它其实是一个懒加载
    nsrunloop *currentrunloop = [nsrunloop currentrunloop];

    //创建一个定时器
    //该方法内部自动添加到runloop中,并且设置运行模式为默认
    [nstimer scheduledtimerwithtimeinterval:1.0 target:self selector:@selector(run) userinfo:nil repeats:yes];

    //让 runloop 运行
    [currentrunloop run];
}

除此之外,我们上面scheduledtimerwithtimeinterval,该方法内部自动添加到runloop中,并且设置运行模式为默认,它内部其实已经调用了[nsrunloop currentrunloop]。那么我们直接运行就行了。于是又有

-(void)timer3{
    //创建一个定时器
    //该方法内部自动添加到runloop中,并且设置运行模式为默认。那么我们可以知道,它内部其实已经调用了[nsrunloop currentrunloop]
    [nstimer scheduledtimerwithtimeinterval:1.0 target:self selector:@selector(run) userinfo:nil repeats:yes];

    [[nsrunloop currentrunloop] run];
}

五、gcd定时器——不受runloop的mode的影响

nstimer 会受到 runloop的mode的影响。然而gcd不会。

5.1 gcd定时器的使用。


-(void)gcdtimer{
    nslog(@"%s",__func__);
   //1.创建gcd中的定时器
   /*
     第一个参数:source的类型dispatch_source_type_timer 表示是定时器
     第二个参数:描述信息,线程id
     第三个参数:更详细的描述信息
     第四个参数:队列,决定gcd定时器中的任务在哪个线程中执行
     */
    dispatch_source_t timer = dispatch_source_create(dispatch_source_type_timer, 0, 0, dispatch_get_global_queue(0, 0));

    //2.设置定时器(起始时间|间隔时间|精准度)
    /*
       第一个参数:定时器对象
       第二个参数:起始时间,dispatch_time_now 从现在开始计时
       第三个参数:间隔时间 1.0 gcd中时间单位为纳秒
       第四个参数:精准度 绝对精准0
       */
    dispatch_source_set_timer(timer, dispatch_time_now, 1.0 * nsec_per_sec, 0 * nsec_per_sec);

    //3.设置定时器执行的任务
    dispatch_source_set_event_handler(timer, ^{
        nslog(@"gcd---%@",[nsthread currentthread]);
    });

    //4.启动执行
    dispatch_resume(timer);
    self.timer = timer;
}

5.2 gcd定时器补充


dispatch_source_type_timer         定时响应(定时器事件)
dispatch_source_type_signal        接收到unix信号时响应

dispatch_source_type_read          io操作,如对文件的操作、socket操作的读响应
dispatch_source_type_write         io操作,如对文件的操作、socket操作的写响应
dispatch_source_type_vnode         文件状态监听,文件被删除、移动、重命名
dispatch_source_type_proc         进程监听,如进程的退出、创建一个或更多的子线程、进程收到unix信号

下面两个都属于mach相关事件响应
    dispatch_source_type_mach_send
    dispatch_source_type_mach_recv

下面两个都属于自定义的事件,并且也是有自己来触发
    dispatch_source_type_data_add
    dispatch_source_type_data_or