zoukankan      html  css  js  c++  java
  • iOS开发总结(A0) - NStimer

    NStimer是ios开发的计时器,简单易用,但有几个注意事项

    1. 创建NStimer的两个常用方法是

    + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;
    + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;

    第一个创建的timer,需要手动加到runloop中去,否则timer 不起作用,加到runloop的方法是:

        [[NSRunLoop mainRunLoop] addTimer:timer forMode:RunLoopMode];

    或者

        [[NSRunLoop currentRunLoop] addTimer:timer forMode:RunLoopMode];

    mainRunloop和currentRunLopp的区别是

    - mainRunloop是主线程的runloop

    - currentRunLopp是当前线程的runloop

    RunLoopMode有两种:NSDefaultRunLoopMode和NSRunLoopCommonModes,区别:

    - NSDefaultRunLoopMode, 如tableview在滚动时,timer失效

    NSRunLoopCommonModes,tableview滚动时,timer不失效

    更多信息,参考 

    http://www.cnblogs.com/zhangjie/p/3727469.html

    http://www.cnblogs.com/zhangjie/p/3727485.html

    第二个创建的timer,系统自动加到runloop中去( current run loop in the default mode)

    2. timer 是retain target 的,只有timer invalidate后,才释放target. 

    对于不重复的timer,执行一次之后timer自动失效,对于重复的timer,需要手动[timer invalidate];

     

    3. timer 时间间隔是大概的时间,要准确及时,可使用CADisplayLink(参考http://www.cnblogs.com/YouXianMing/p/4029547.html)和 GCD(后续将专门学习总结GCD)

     

  • 相关阅读:
    Linux内核中锁机制之RCU、大内核锁
    Linux内核中锁机制之完成量、互斥量
    Linux内核中锁机制之信号量、读写信号量
    Linux内核中锁机制之内存屏障、读写自旋锁及顺序锁
    Linux内核中锁机制之原子操作、自旋锁
    Linux内核jiffies简介
    pdflush机制
    ext2文件系统
    从ext2文件系统上读出超级块
    ext2磁盘布局
  • 原文地址:https://www.cnblogs.com/beddup/p/4614664.html
Copyright © 2011-2022 走看看