NSTimer初始化及注意事项

来源:互联网 发布:hyper v vmware 知乎 编辑:程序博客网 时间:2024/04/30 03:08

一 : 什么是NSTimer?

官方文档说“A timer provides a way to perform a delayed action or a periodic action. The timer waits until a certain time interval has elapsed and then fires, sending a specified message to a specified object. ” 意思就是timer就是一个能在从现在开始的后面的某一个时刻或者周期性的执行我们指定的方法的对象。从官方给出的解释可以看出timer会在未来的某个时刻执行一次或者多次我们指定的方法,这也就牵扯出一个问题,如何保证timer在未来的某个时刻触发指定事件的时候,我们指定的方法是有效的呢?

  解决方法很简单,只要将指定给timer的方法的接收者retain一份就搞定了,实际上系统也是这样做的。不管是重复性的timer还是一次性的timer都会对它的方法的接收者进行retain,这两种timer的区别在于“一次性的timer在完成调用以后会自动将自己invalidate,而重复的timer则将永生,直到你显示的invalidate它为止”。

二 :NSTimer和NSRunLoop的关系?

只要出现NSTimer必须要有NSRunLoop,NSTimer必须依赖NSRunLoop才能执行 。NSTimer其实也是一种资源,如果看过多线程编程指引文档的话,我们会发现所有的source如果要起作用,就得加到runloop中去。同理timer这种资源要想起作用,那肯定也需要加到runloop中才会生效喽。如果一个runloop里面不包含任何资源的话,运行该runloop时会立马退出。你可能会说那我们APP的主线程的runloop我们没有往其中添加任何资源,为什么它还好好的运行。我们不添加,不代表框架没有添加,如果有兴趣的话你可以打印一下main thread的runloop,你会发现有很多资源。 

三 :NSTimer主要方法一览

+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo; //生成timer但不执行

+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo; //生成timer并且纳入当前线程的run loop来执行,不需要用addTimer方法。

  1. //关闭 - 永久关闭
  2. [timer invalidate];  

  1. //暂时定时器  
  2. [myTimer setFireDate:[NSDate distantFuture]];  

  1. //开启定时器  
  2. [myTimer setFireDate:[NSDate distantPast]];  

NSRunLoop与timer有关方法为:

- (void)addTimer:(NSTimer *)timer forMode:(NSString *)mode; //在run loop上注册timer

主线程已经有run loop,所以NSTimer一般在主线程上运行都不必再调用addTimer:。但在非主线程上运行必须配置run loop


四 : NSRunLoop主要方法一览

+ (NSRunLoop *)currentRunLoop; //获得当前线程的run loop

+ (NSRunLoop *)mainRunLoop; //获得主线程的run loop

- (void)run; //进入处理事件循环,如果没有事件则立刻返回。注意:主线程上调用这个方法会导致无法返回(进入无限循环,虽然不会阻塞主线程),因为主线程一般总是会有事件处理。

- (void)runUntilDate:(NSDate *)limitDate; //同run方法,增加超时参数limitDate,避免进入无限循环。使用在UI线程(亦即主线程)上,可以达到暂停的效果。

- (BOOL)runMode:(NSString *)mode beforeDate:(NSDate *)limitDate; //等待消息处理,好比在PC终端窗口上等待键盘输入。一旦有合适事件(mode相当于定义了事件的类型)被处理了,则立刻返回;类同run方法,如果没有事件处理也立刻返回;有否事件处理由返回布尔值判断。同样limitDate为超时参数。

- (void)acceptInputForMode:(NSString *)mode beforeDate:(NSDate *)limitDate; //似乎和runMode:差不多(测试过是这种结果,但确定是否有其它特殊情况下的不同),没有BOOL返回值。

官网文档也提到run和runUntilDate:会以NSDefaultRunLoopMode参数调用runMode:来处理事件。

当app运行后,iOS系统已经帮助主线程启动一个run loop,而一般线程则需要手动来启动run loop。

使用run loop的一个好处就是避免线程轮询的开销,run loop在无事件处理时可以自动进入睡眠状态,降低CPU的能耗。

[[NSRunLoop mainRunLoop] run]; //主线程永远等待,但让出主线程时间片

[[NSRunLoop mainRunLooprunUntilDate:[NSDate distantFuture]]; //等同上面调用

[[NSRunLoop mainRunLooprunUntilDate:[NSDate date]]; //立即返回

[[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:10.0]]; //主线程等待,但让出主线程时间片,然后过10秒后返回

[[NSRunLoop mainRunLoop] runMode:NSDefaultRunLoopMode beforeDate: [NSDate distantFuture]]; //主线程等待,但让出主线程时间片;有事件到达就返回,比如点击UI等。

[[NSRunLoop mainRunLooprunMode:NSDefaultRunLoopMode beforeDate: [NSDate date]]; //立即返回

[[NSRunLoop mainRunLoop] runMode:NSDefaultRunLoopMode beforeDate: [NSDate dateWithTimeIntervalSinceNow:10.0]]; //主线程等待,但让出主线程时间片;有事件到达就返回,如果没有则过10秒返回。

五 : 注意事项

我们通常在主线程中使用NSTimer,有个实际遇到的问题需要注意。当滑动界面时,系统为了更好地处理UI事件和滚动显示,主线程runloop会暂时停止处理一些其它事件,这时主线程中运行的NSTimer就会被暂停。解决办法就是改变NSTimer运行的mode(mode可以看成事件类型),不使用缺省的NSDefaultRunLoopMode,而是改用NSRunLoopCommonModes,这样主线程就会继续处理NSTimer事件了。具体代码如下:

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

[[NSRunLoop currentRunLoopaddTimer:timer forMode:NSRunLoopCommonModes];

  //创建一个定时器
       _timer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(changeTimeAtTimedisplay) userInfo:nil repeats:YES];




_timer=[NSTimer timerWithTimeInterval:10 target:self selector:@selector(changeTimeAtTimedisplay) userInfo:nil repeats:YES];
       //必须手动加入到当前循环中去
       NSRunLoop *runloop=[NSRunLoop currentRunLoop];
       [runloop addTimer:_timer forMode:NSDefaultRunLoopMode];


以上两端代码效果是一样的



,NSTimer一定准确么?

NSTimer其实并不是一个实时的系统,正常情况下它能按照指定的周期触发,但如果当前线程有阻塞的时候会延迟执行,在延迟超过一个周期时会和下一个触发合并在下一个触发时刻执行。除此之外,多线程程序实际上也是要在CPU的处理上同步进行,操作系统并不能保证多线程严格同步。一个很典型的场景就是:如果我们定义一个一秒周期的定时器,希望它保持一秒计数,当计时的时间越来越长的时候,误差会越来越大。


,如何在使NSTimer在后台也能执行?

正常情况下,NSTimer会在应用进入后台时停止工作,进入前台时又重新计时。那么怎么使NSTimer在后台也能执行呢?

要完成这个需求,就要借助苹果上的音频播放类在后台执行的这个特权。具体操作方法如下:

Info.plist中,添加"Required background modes"数组键,设置一个元素为"App plays audio".

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中添加:

NSError *err = nil;

[[AVAudioSession sharedInstance]setCategory: AVAudioSessionCategoryPlayback error: &err];

[[AVAudioSession sharedInstance]setActive: YES error: &err];

再添加如下方法:

折叠C/C++ Code复制内容到剪贴板

  • (void)applicationDidEnterBackground:(UIApplication *)application{  
  •     UIApplication*   app [UIApplication sharedApplication];  
  •     __block    UIBackgroundTaskIdentifier bgTask;  
  •     bgTask [app beginBackgroundTaskWithExpirationHandler:^{  
  •         dispatch_async(dispatch_get_main_queue(), ^{  
  •             if (bgTask != UIBackgroundTaskInvalid)  
  •              
  •                 bgTask UIBackgroundTaskInvalid;  
  •              
  •         });  
  •     }];  
  •     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{  
  •         dispatch_async(dispatch_get_main_queue(), ^{  
  •             if (bgTask != UIBackgroundTaskInvalid)  
  •              
  •                 bgTask UIBackgroundTaskInvalid;  
  •              
  •         });  
  •     });  
  • }  

还有一种牺牲页面流畅性的方法,直接在主线程中,提高timer的runloop权限,不过建议为了用户体验,还是放弃这种方法。

    if (nil == self.updateTimer)

    {

    self.updateTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];

        [[NSRunLoop currentRunLoop] addTimer:self.updateTimer forMode:NSRunLoopCommonModes];

    }





八,关于这个方法:

Firing a Timer
– fire


其实他并不是真的启动一个定时器,从之前的初始化方法中我们也可以看到,建立的时候,在适当的时间,定时器就会自动启动。那这个方法是干什么的呢。

You can use this method to fire a repeating timer without interrupting its regular firing schedule. If the timer is non-repeating, it is automatically invalidated after firing, even if its scheduled fire date has not arrived.

这是官方文档的说法,英文说的很清楚,但我们理解还不是很到位,为了彻底搞懂它的功能。我又做了一个测试。


也是很简单,把上面那个定时器,改变一点

//初始化的时候创建一个定时器

- (id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    
    if (self) {
        // Custom initialization
        
        //创建一个定时器,
        
       _timer=[NSTimer timerWithTimeInterval:10 target:self selector:@selector(changeTimeAtTimedisplay) userInfo:nil repeats:YES];
      

//手动加入到循环中

      NSRunLoop *runloop=[NSRunLoop currentRunLoop];

 [runloop addTimer:_timer forMode:NSDefaultRunLoopMode];


//当然这个定时器会自动启动,只不多过了十秒之后,才触发

}

return self

}



当我们单击“开始”按钮时,


- (IBAction)startTime:(id)sender {
   
    //只是简单地调用一下这个方法,看到底功能是什么
  [_timer fire];

}

结果是,单击一下按钮,倒计时减1,单击一下减1,即它把触发的时间给提前了,但过十秒后倒计时还会减1,即它只是提前触发定时器,而不影响之前的那个定时器设置的时间,就好比我们等不及要去看一场球赛,赶紧把车开快些一样,fire的功能就像让我们快些到球场,但却不影响球赛开始的时间。

还记得之前那个初始化定时器时,设置的是YES吗,当我们,改为NO时,即不让它循环触发时,我们此时再单击开始按钮。会猛然发现,倒计时减1了,但当我们再点击开始按钮时,会发现倒计时,不会动了。原因是:我们的定时器,被设置成只触发一次,再fire的时候,触发一次,该定时器,就被自动销毁了,以后再fire也不会触发了。


现在 我们再看官方的解释,或许就会更明白了,



You can use this method to fire a repeating timer without interrupting its regular firing schedule. If the timer is non-repeating, it is automatically invalidated after firing, even if its scheduled fire date has not arrived.

这就对了,fire并不是启动一个定时器,只是提前触发而已。

0 0
原创粉丝点击