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的初始化方法將以默認(rèn)mode直接添加到當(dāng)前的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、觸發(fā)(啟動)
當(dāng)定時器創(chuàng)建完(不用scheduled的,添加到runloop中后,該定時器將在初始化時指定的timeInterval秒后自動觸發(fā)。
NSTimer*timer=[NSTimertimerWithTimeInterval:0.5target:selfselector:@selector(timeSchedule)userInfo:nilrepeats:YES];NSRunLoop*runLoop=[NSRunLoopcurrentRunLoop]; [runLoopaddTimer:timerforMode:NSDefaultRunLoopMode];[timer fire];
可以使用-(void)fire;方法來立即觸發(fā)該定時器;
3、停止
- (void)invalidate;
這個是唯一一個可以將計(jì)時器從runloop中移出的方法。需要將定時器清空,如
- (void)invalidateTimer
{
if(_timer) {
[_timer invalidate];
_timer = nil;
}
}
4、在多線程開發(fā)中,如果是在mainthread中使用定時器,兩種初始化方法都能使用,'如果是在子線程中使用定時器,只能使用方法:
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;
并且啟動定時器不能用fire,只能讓runloop一直執(zhí)行下去,sample code:
_timer = [NSTimer timerWithTimeInterval:0.5 target:selfselector:@selector(timeSchedule)userInfo:nilrepeats:YES];NSRunLoop*runLoop=[NSRunLoopcurrentRunLoop];[runLoopaddTimer:_timerforMode:NSDefaultRunLoopMode];
BOOL shouldKeepRunning = YES;//global
while (shouldKeepRunning && [runloop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]);
下面解釋一下,兩種初始化方法使用為什么有這樣的區(qū)別
在使用NSTimer的時候遇到過到了設(shè)定的時間NSTimer指定的方法不執(zhí)行的情況,發(fā)現(xiàn)調(diào)用NSTimer不是在主線程,需要將NSTimer添加到NSRunloop中。
下面根據(jù)自己實(shí)際開發(fā)總結(jié)使用NSTimer需要注意的問題.
一.調(diào)用NSTimer會對調(diào)用的對象retain,不管是重復(fù)的NSTimer還是一次性的NSTimer都會對創(chuàng)建該NSTimer的對象進(jìn)行retain操作。一次性的NSTimer會在設(shè)定時間到來時完成調(diào)用然后將自己invalidate,而重復(fù)性的NSTimer只有開發(fā)者調(diào)用invalidate時才會停止。鑒于此,在使用NSTimerd的時候一定不要忘記在恰當(dāng)?shù)臅r候執(zhí)行invalidate操作,否則對于不執(zhí)行invalidate操作的重復(fù)性NSTimer的,會造成對象不能釋放,發(fā)生內(nèi)存泄漏。
二.NSTimer必須加入NSRunloop中才能正確執(zhí)行.如果在非主線程的線程中只是創(chuàng)建一個NSTimer并啟動,該NSTimer是不會執(zhí)行的,除非將NSTimer加入到該線程的NSRunloop中,并啟動NSRunloop才行。示例如下:
[[NSRunLoop currentRunLoop]addTimer:timerforMode:NSDefaultRunLoopMode];
[[NSRunLoop currentRunLoop] run]];
也許有朋友會說:我在主線程中創(chuàng)建了NSTimer,也并沒有加入NSRunloop中,怎么就能正確執(zhí)行呢?這是因?yàn)橹骶€程默認(rèn)創(chuàng)建好了NSRunloop,如果你使用如下方法打印主線程的NSRunloop信息會看到主線程的NSRunloop里面的信息非常多,是默認(rèn)創(chuàng)建好的。
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只有在調(diào)用[NSRunloopcurrentRunloop]才會創(chuàng)建。
三.NSTimer一定準(zhǔn)確么?
NSTimer其實(shí)并不是一個實(shí)時的系統(tǒng),正常情況下它能按照指定的周期觸發(fā),但如果當(dāng)前線程有阻塞的時候會延遲執(zhí)行,在延遲超過一個周期時會和下一個觸發(fā)合并在下一個觸發(fā)時刻執(zhí)行。除此之外,多線程程序?qū)嶋H上也是要在CPU的處理上同步進(jìn)行,操作系統(tǒng)并不能保證多線程嚴(yán)格同步。一個很典型的場景就是:如果我們定義一個一秒周期的定時器,希望它保持一秒計(jì)數(shù),當(dāng)計(jì)時的時間越來越長的時候,誤差會越來越大。
四,如何在使NSTimer在后臺也能執(zhí)行?
正常情況下,NSTimer會在應(yīng)用進(jìn)入后臺時停止工作,進(jìn)入前臺時又重新計(jì)時。那么怎么使NSTimer在后臺也能執(zhí)行呢?****要完成這個需求,就要借助蘋果上的音頻播放類在后臺執(zhí)行的這個特權(quán)。具體操作方法如下:****在Info.plist中,添加"Requiredbackground modes"數(shù)組鍵,設(shè)置一個元素為"Appplays audio".
在-(BOOL)application:(UIApplication)applicationdidFinishLaunchingWithOptions:(NSDictionary)launchOptions方法中添加:****NSError *err = nil;****[[AVAudioSession sharedInstance]setCategory:AVAudioSessionCategoryPlayback error: &err];[[AVAudioSession sharedInstance]setActive: YES error:&err]```再添加如下方法:
- (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權(quán)限,不過建議為了用戶體驗(yàn),還是放棄這種方法。
if (nil== self.updateTimer) {
self.updateTimer = [NSTimer scheduledTimerWithTimeInterval:1target:selfselector:@selector(updateTime) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop]addTimer:self.updateTimer forMode:NSRunLoopCommonModes];
}
[轉(zhuǎn)][http://blog.sina.com.cn/s/blog_8280f5ec0101my1v.html]