iOS 学习笔记iOS菜鸟食谱锻炼吃饭的家伙

NSTimer使用注意点

2016-02-29  本文已影响3004人  未来可期me

NSTimer在代码中的使用

1、初始化

+ (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;

注意:userInfo是值NSTimer携带的用户信息。
不用scheduled方式初始化的,需要手动addTimer:forMode: 将timer添加到一个runloop中。而scheduled的初始化方法将以默认mode直接添加到当前的runloop中

[NSTimer scheduledTimerWithTimeInterval:2 target:selfselector:@selector(startFindApartment:) userInfo:nil repeats:YES];

NSTimer *myTimer = [NSTimer timerWithTimeInterval:3.0 target:selfselector:@selector(timerFired:) userInfo:nilrepeats:NO];
[[NSRunLoopcurrentRunLoop] addTimer:myTimerforMode:NSDefaultRunLoopMode];

2、触发(启动)

当定时器创建完(不用scheduled的,添加到runloop中后,该定时器将在初始化时指定的timeInterval秒后自动触发。

NSTimer*timer=[NSTimertimerWithTimeInterval:0.5target:selfselector:@selector(timeSchedule)userInfo:nilrepeats:YES];
NSRunLoop*runLoop=[NSRunLoopcurrentRunLoop]; [runLoopaddTimer:timerforMode:NSDefaultRunLoopMode];
[timer fire];

可以使用-(void)fire;方法来立即触发该定时器;

3、停止

- (void)invalidate;

这个是唯一一个可以将计时器从runloop中移出的方法。

4、在多线程开发中,如果是在mainthread中使用定时器,两种初始化方法都能使用,'如果是在子线程中使用定时器,只能使用方法:'

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

并且启动定时器不能用fire,只能让runloop一直执行下去,sample code:

_timer = [NSTimer timerWithTimeInterval:0.5 target:selfselector:@selector(timeSchedule)userInfo:nilrepeats:YES];
NSRunLoop*runLoop=[NSRunLoopcurrentRunLoop];
[runLoopaddTimer:_timerforMode:NSDefaultRunLoopMode];
while([runLooprunMode:NSDefaultRunLoopModebeforeDate[NSDatedistantFuture]]);

下面解释一下,两种初始化方法使用为什么有这样的区别

**
在使用NSTimer的时候遇到过到了设定的时间NSTimer指定的方法不执行的情况,发现调用NSTimer不是在主线程,需要将NSTimer添加到NSRunloop中。下面特酷吧根据自己实际开发总结使用NSTimer需要注意的问题.
**

一,调用NSTimer会对调用的对象retain
**
不管是重复的NSTimer还是一次性的NSTimer都会对创建该NSTimer的对象进行retain操作。一次性的NSTimer会在设定时间到来时完成调用然后将自己invalidate,而重复性的NSTimer只有开发者调用invalidate时才会停止。鉴于此,在使用NSTimerd的时候一定不要忘记在恰当的时候执行invalidate操作,否则对于不执行invalidate操作的重复性NSTimer的,会造成对象不能释放,发生内存泄漏。
**

二,NSTimer必须加入NSRunloop中才能正确执行
如果在非主线程的线程中只是创建一个NSTimer并启动,该NSTimer是不会执行的,除非将NSTimer加入到该线程的NSRunloop中,并启动NSRunloop才行。示例如下:

[[NSRunLoop currentRunLoop]addTimer:timer forMode:NSDefaultRunLoopMode];  
[[NSRunLoop currentRunLoop] run]]; 

也许有朋友会说:我在主线程中创建了NSTimer,也并没有加入NSRunloop中,怎么就能正确执行呢?这是因为主线程默认创建好了NSRunloop,如果你使用如下方法打印主线程的NSRunloop信息会看到主线程的NSRunloop里面的信息非常多,是默认创建好的。

NSLog(@"main Runloop %@",[NSRunLoop mainRunLoop]);

{wakeup port = 0x1e03, stopped = false, ignoreWakeUps =true,
current mode = GSEventReceiveRunLoopMode,
common modes = {type = mutable set, count = 2,
entries =>
** 0 :{contents = "UITrackingRunLoopMode"}**
** 1 :{contents = "kCFRunLoopDefaultMode"}**
}
除了主线程之外,其他线程的NSRunloop只有在调用[NSRunloop currentRunloop]才会创建。


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


四,如何在使NSTimer在后台也能执行?
正常情况下,NSTimer会在应用进入后台时停止工作,进入前台时又重新计时。那么怎么使NSTimer在后台也能执行呢?
要完成这个需求,就要借助苹果上的音频播放类在后台执行的这个特权。具体操作方法如下:
在Info.plist中,添加"Requiredbackground modes"数组键,设置一个元素为"Appplays audio".
在-(BOOL)application:(UIApplication)applicationdidFinishLaunchingWithOptions:(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:selfselector:@selector(updateTime) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop]addTimer:self.updateTimer forMode:NSRunLoopCommonModes];<code>
}

[转][http://blog.sina.com.cn/s/blog_8280f5ec0101my1v.html]

上一篇下一篇

猜你喜欢

热点阅读