初探NSRunLoop

作者: iOSUI拖拽工程师 | 来源:发表于2017-04-07 20:32 被阅读47次

    RunLoop 简介

    RunLoop 接收输入事件有两种不同的源:输入源和定时源。输入源传递异步事件,通常消息来自其他线程和程序。定时源则传递同步事件,发生在重复的时间或者重复的时间间隔。


    1.runloop 初探

    runloop,顾名思义,是一个循环。有事件去处理的时候就去处理,没事件处理的时候就休息。

    本文结合NSTimer来初步学习runloop

    场景    在runloop的默认模式下添加一个timer,然后加一个UI控件 

    NSTimer*timer = [NSTimerscheduledTimerWithTimeInterval:1.0target:selfselector:@selector(timerMethod)userInfo:nilrepeats:YES];

    - (void)timerMethod

    {

    staticint num =0;

    NSLog(@"%@ %d",[NSThreadcurrentThread],num++);

    }

    运行后,timer每秒打印一次,打印可以知道timer加在了主线程。担当拖动UI控件的时候,timer停止打印了。原因并不是阻塞了主线程(如果阻塞了主线程,那么UI控件也不会动了)。真正原因是:timer执行的时候,runloop在默认模式下执行timer。拖动界面的时候(source源),runloop在UI模式下去执行UI事件,拖住控件不松手,runloop就会一直处理UI事件,不再去处理timer源事件。

    runloop的两种重要模式:

    (1)NSDefaultRunLoopMode runloop的默认模式,只要有事件就处理

    (2)UITrackingRunLoopMode (优先切换)这个模式,是在有UI事件的时候切换到的模式

    备注:NSRunLoopCommonModes  占位符,不算是一种模式(默认模式和UI模式的结合)

    尝试解决办法1:把timer放在UI模式下

    NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(timerMethod) userInfo:nil repeats:YES];                                         [[NSRunLoop currentRunLoop]addTimer:timer1 forMode:NSRunLoopCommonModes];

    此时再运行,发现每秒打印一次没问题。貌似解决了问题。但是,并不完美。

    如果timerMethod里面做了耗时操作,会有什么结果呢?修改timerMethod方法如下

    - (void)timerMethod

    {

    [NSThread sleepForTimeInterval:1.0];

    staticint num =0;

    NSLog(@"%@ %d",[NSThreadcurrentThread],num++);

    }

    这是再运行,我们会发现,即使拖动UI控件也没问题,每秒会打印一次。但是由于有耗时操作,由于耗时操作是在主线程,所以耗时操作的时候UI控件会卡顿。

    尝试解决方案2   把timer放在子线程

    NSThread *thread = [[NSThread alloc]initWithBlock:^{

        NSTimer *timer1 = [NSTimer timerWithTimeInterval:1.0 target:self        selector:@selector(timerMethod) userInfo:nil repeats:YES];

        [[NSRunLoop currentRunLoop]addTimer:timer1 forMode:NSRunLoopCommonModes];

    }];

        [thread start];

    思考:timerMethod会执行吗?

    运行后发现:timerMethod并没有被执行。原因是子线程,执行完任务被回收,所以不会执行timerMethod方法。子线程被回收是因为,子线程的runloop默认是不开启的。

    下面做如下修改

    NSThread *thread = [[NSThread alloc]initWithBlock:^{

        NSTimer *timer1 = [NSTimer timerWithTimeInterval:1.0 target:self     selector:@selector(timerMethod) userInfo:nil repeats:YES];

        [[NSRunLoop currentRunLoop]addTimer:timer1 forMode:NSRunLoopCommonModes];

        [[NSRunLoop currentRunLoop]run];

        NSLog(@"timer初始化%@",[NSThread currentThread]); //这个会执行吗????

    }];

    [thread start];

    猜想:打印timer初始化的那句代码会被执行吗?

    答案是否定的。因为[[NSRunLoop currentRunLoop]run]; 这句代码是一个死循环。进入处理事件循环,如果没有事件则立刻返回,因为一直有timer事件,所以一直无法返回。

    尝试解决方案3  自己管理runloop循环

    _finished 为属性 初始值为yes

    - (void)runUntilDate:(NSDate*)limitDate;//同run方法,增加超时参数limitDate,避免进入无限循环。

    NSThread*thread = [[NSThreadalloc]initWithBlock:^{

        NSTimer*timer1 =      [NSTimer     timerWithTimeInterval:1.0 target:self selector:@selector(timerMethod) userInfo:nil repeats:YES];

    [[NSRunLoop currentRunLoop]addTimer:timer1 forMode:NSRunLoopCommonModes];

    while (_finished) {

    [[NSRunLoop currentRunLoop]runUntilDate:[NSDate dateWithTimeIntervalSinceReferenceDate:0.01]];// 每0.01秒开始一个runloop,超过0.01秒。runloop自动退出。

    }

    NSLog(@"timer初始化%@",[NSThread currentThread]);//这个会执行吗???

    }];

    [thread start];

    NSLog(@"main thread");//这句主线程运行的,可以执行

    }

    为了使runloop退出  需要在适当的地方修改finished为no,这样就可以退出while循环

    源码:https://github.com/mhlee0514/RunLoop

    参考资料:http://www.cnblogs.com/tangbinblog/archive/2012/12/07/2807088.html

    相关文章

      网友评论

        本文标题:初探NSRunLoop

        本文链接:https://www.haomeiwen.com/subject/tpbiottx.html