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方式初始化的铝噩,需要手動(dòng)addTimer:forMode: 將timer添加到一個(gè)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))
當(dāng)定時(shí)器創(chuàng)建完(不用scheduled的具被,添加到runloop中后一姿,該定時(shí)器將在初始化時(shí)指定的timeInterval秒后自動(dòng)觸發(fā)。
NSTimer*timer=[NSTimertimerWithTimeInterval:0.5target:selfselector:@selector(timeSchedule)userInfo:nilrepeats:YES];
NSRunLoop*runLoop=[NSRunLoopcurrentRunLoop]; [runLoopaddTimer:timerforMode:NSDefaultRunLoopMode];
[timer fire];
可以使用-(void)fire;方法來(lái)立即觸發(fā)該定時(shí)器艾栋;
3蛉顽、停止
- (void)invalidate;
這個(gè)是唯一一個(gè)可以將計(jì)時(shí)器從runloop中移出的方法。
4悼粮、在多線程開發(fā)中曾棕,如果是在mainthread中使用定時(shí)器,兩種初始化方法都能使用申尤,'如果是在子線程中使用定時(shí)器子眶,只能使用方法:'
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;
并且啟動(dòng)定時(shí)器不能用fire臭杰,只能讓runloop一直執(zhí)行下去谚中,sample code:
_timer = [NSTimer timerWithTimeInterval:0.5 target:selfselector:@selector(timeSchedule)userInfo:nilrepeats:YES];
NSRunLoop*runLoop=[NSRunLoopcurrentRunLoop];
[runLoopaddTimer:_timerforMode:NSDefaultRunLoopMode];
while([runLooprunMode:NSDefaultRunLoopModebeforeDate[NSDatedistantFuture]]);
下面解釋一下,兩種初始化方法使用為什么有這樣的區(qū)別
**
在使用NSTimer的時(shí)候遇到過(guò)到了設(shè)定的時(shí)間NSTimer指定的方法不執(zhí)行的情況,發(fā)現(xiàn)調(diào)用NSTimer不是在主線程,需要將NSTimer添加到NSRunloop中磁奖。下面特酷吧根據(jù)自己實(shí)際開發(fā)總結(jié)使用NSTimer需要注意的問(wèn)題.
**
一,調(diào)用NSTimer會(huì)對(duì)調(diào)用的對(duì)象retain
**
不管是重復(fù)的NSTimer還是一次性的NSTimer都會(huì)對(duì)創(chuàng)建該NSTimer的對(duì)象進(jìn)行retain操作比搭。一次性的NSTimer會(huì)在設(shè)定時(shí)間到來(lái)時(shí)完成調(diào)用然后將自己invalidate,而重復(fù)性的NSTimer只有開發(fā)者調(diào)用invalidate時(shí)才會(huì)停止。鑒于此,在使用NSTimerd的時(shí)候一定不要忘記在恰當(dāng)?shù)臅r(shí)候執(zhí)行invalidate操作,否則對(duì)于不執(zhí)行invalidate操作的重復(fù)性NSTimer的,會(huì)造成對(duì)象不能釋放,發(fā)生內(nèi)存泄漏蜜托。
**
二,NSTimer必須加入NSRunloop中才能正確執(zhí)行
如果在非主線程的線程中只是創(chuàng)建一個(gè)NSTimer并啟動(dòng),該NSTimer是不會(huì)執(zhí)行的,除非將NSTimer加入到該線程的NSRunloop中,并啟動(dòng)NSRunloop才行霉赡。示例如下:
[[NSRunLoop currentRunLoop]addTimer:timer forMode:NSDefaultRunLoopMode];
[[NSRunLoop currentRunLoop] run]];
也許有朋友會(huì)說(shuō):我在主線程中創(chuàng)建了NSTimer,也并沒有加入NSRunloop中,怎么就能正確執(zhí)行呢?這是因?yàn)橹骶€程默認(rèn)創(chuàng)建好了NSRunloop,如果你使用如下方法打印主線程的NSRunloop信息會(huì)看到主線程的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)用[NSRunloop currentRunloop]才會(huì)創(chuàng)建嗓化。
三,NSTimer一定準(zhǔn)確么?
NSTimer其實(shí)并不是一個(gè)實(shí)時(shí)的系統(tǒng),正常情況下它能按照指定的周期觸發(fā),但如果當(dāng)前線程有阻塞的時(shí)候會(huì)延遲執(zhí)行,在延遲超過(guò)一個(gè)周期時(shí)會(huì)和下一個(gè)觸發(fā)合并在下一個(gè)觸發(fā)時(shí)刻執(zhí)行。除此之外,多線程程序?qū)嶋H上也是要在CPU的處理上同步進(jìn)行,操作系統(tǒng)并不能保證多線程嚴(yán)格同步故爵。一個(gè)很典型的場(chǎng)景就是:如果我們定義一個(gè)一秒周期的定時(shí)器,希望它保持一秒計(jì)數(shù),當(dāng)計(jì)時(shí)的時(shí)間越來(lái)越長(zhǎng)的時(shí)候,誤差會(huì)越來(lái)越大隅津。
四,如何在使NSTimer在后臺(tái)也能執(zhí)行?
正常情況下,NSTimer會(huì)在應(yīng)用進(jìn)入后臺(tái)時(shí)停止工作,進(jìn)入前臺(tái)時(shí)又重新計(jì)時(shí)。那么怎么使NSTimer在后臺(tái)也能執(zhí)行呢?
要完成這個(gè)需求,就要借助蘋果上的音頻播放類在后臺(tái)執(zhí)行的這個(gè)特權(quán)结窘。具體操作方法如下:
在Info.plist中充蓝,添加"Requiredbackground modes"數(shù)組鍵谓苟,設(shè)置一個(gè)元素為"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復(fù)制內(nèi)容到剪貼板
- (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;
}
});
});
}
還有一種犧牲頁(yè)面流暢性的方法涝焙,直接在主線程中,提高timer的runloop權(quán)限仑撞,不過(guò)建議為了用戶體驗(yàn),還是放棄這種方法桶良。
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>
}
[轉(zhuǎn)][http://blog.sina.com.cn/s/blog_8280f5ec0101my1v.html]