NSTimer与RunLoop

14 篇文章 0 订阅

今天在论坛里看见有人提出这样一个问题:界面上有个scrollView 每次滑动的时候,NSTimer就停止了,为什么?

看了下,下面的回复不少,但我感觉都没有真正解释清其中的问题,下面我来试着写一下我个人的理解

(我基本上都是通过下面这篇文章学习的,有兴趣更深入、详细了解runloop的同学可以看看

点击打开链接)


首先,一个runloop下会包含很多个model,每个model下又会包含很多的timer/source/observe,同一时刻runloop只能在一种模式下运行,处理一种模式下的状态

所以层次关系是    runloop 包含 model 包含 timer/source/observe   

阅读下面内容时,希望你时刻意识到这个层次关系,以及mode的间“互斥”


来看看App启动后main runloop的状态

CFRunLoop {
    current mode = kCFRunLoopDefaultMode
    
    common modes = {
        UITrackingRunLoopMode
        kCFRunLoopDefaultMode
    }
    
    common mode items = {
        source1,
        source2,
        timer1,
        timer2,
        observer1,
        observer2
        .........
    },
    
    modes = {
        CFRunLoopMode  {
            sources0 =  { /* same as 'common mode items' */ },
            sources1 =  { /* same as 'common mode items' */ },
            observers = { /* same as 'common mode items' */ },
            timers =    { /* same as 'common mode items' */ },
        },
        
        CFRunLoopMode  {
            sources0 =  { /* same as 'common mode items' */ },
            sources1 =  { /* same as 'common mode items' */ },
            observers = { /* same as 'common mode items' */ },
            timers =    { /* same as 'common mode items' */ },
        },
        
        CFRunLoopMode  {
            ……………..
        },
        
        CFRunLoopMode  {
            ………………..
        },
        
        CFRunLoopMode  {
            …………….
        }
    }
    }

先看modes,确实如上所述,main runloop里包含了5个mode,每个mode里有含有很多的source、observers及timers


接着注意main loop还包含的common modes以及common mode items两个属性

common modes里面包含了一组mode(针对的是mode层级)
common mode items(代码被我简写了)里则包含了一些timer、observer及source(针对的是timer、observer及source层级)


什么意思呢?

1.mode可以将自己标记为为common,标记为common的mode,都会被添加进common modes中(针对mode层级),系统默认是将kCFRunLoopDefaultMode、UITrackingRunLoopMode都放到common modes中了

2.凡是在common mode items中包含的timer、observer及source,它们的变化及状态,都会被同步到common modes包含的modes中,也就是说它们在多个mode模式下都可以被处理(在common modes中的mode都会处理它们)


基于以上概念,当我们执行

NSTimer *timer1 = [NSTimer timerWithTimeInterval:2 repeats:YES block:^(NSTimer * _Nonnull timer) {
        NSLog(@"test");
    }];
    [[NSRunLoop currentRunLoop] addTimer:timer1 forMode:NSDefaultRunLoopMode];

或者

[NSTimer scheduledTimerWithTimeInterval:2 repeats:YES block:^(NSTimer * _Nonnull timer) {
        NSLog(@"test");
    }];

时,timer只是被加到了kCFRunLoopDefaultMode模式下,当scroll被滑动时,runloop被切换到了UITrackingRunLoopMode模式下,所以timer自然就不工作了同一时刻runloop只能在一种模式下运行,处理一种模式下的状态)


解决方法是

NSTimer *timer1 = [NSTimer timerWithTimeInterval:2 repeats:YES block:^(NSTimer * _Nonnull timer) {
        NSLog(@"test");
    }];
    [[NSRunLoop currentRunLoop] addTimer:timer1 forMode:NSRunLoopCommonModes];

它的作用就是将timer放到了顶层main runloop的common mode items中了,这样kCFRunLoopDefaultMode和UITrackingRunLoopMode(上文说了,它们默认就是在common modes中的)就都会对该timer进行处理了。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值