NSTimer使用注意點(diǎn)

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攜帶的用戶(hù)信息。
不用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、在多線(xiàn)程開(kāi)發(fā)中,如果是在mainthread中使用定時(shí)器,兩種初始化方法都能使用,'如果是在子線(xiàn)程中使用定時(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不是在主線(xiàn)程,需要將NSTimer添加到NSRunloop中。下面特酷吧根據(jù)自己實(shí)際開(kāi)發(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只有開(kāi)發(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í)行
如果在非主線(xiàn)程的線(xiàn)程中只是創(chuàng)建一個(gè)NSTimer并啟動(dòng),該NSTimer是不會(huì)執(zhí)行的,除非將NSTimer加入到該線(xiàn)程的NSRunloop中,并啟動(dòng)NSRunloop才行。示例如下:

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

也許有朋友會(huì)說(shuō):我在主線(xiàn)程中創(chuàng)建了NSTimer,也并沒(méi)有加入NSRunloop中,怎么就能正確執(zhí)行呢?這是因?yàn)橹骶€(xiàn)程默認(rèn)創(chuàng)建好了NSRunloop,如果你使用如下方法打印主線(xiàn)程的NSRunloop信息會(huì)看到主線(xiàn)程的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"}**
}
除了主線(xiàn)程之外,其他線(xiàn)程的NSRunloop只有在調(diào)用[NSRunloop currentRunloop]才會(huì)創(chuàng)建。


三,NSTimer一定準(zhǔn)確么?
NSTimer其實(shí)并不是一個(gè)實(shí)時(shí)的系統(tǒng),正常情況下它能按照指定的周期觸發(fā),但如果當(dāng)前線(xiàn)程有阻塞的時(shí)候會(huì)延遲執(zhí)行,在延遲超過(guò)一個(gè)周期時(shí)會(huì)和下一個(gè)觸發(fā)合并在下一個(gè)觸發(fā)時(shí)刻執(zhí)行。除此之外,多線(xiàn)程程序?qū)嶋H上也是要在CPU的處理上同步進(jìn)行,操作系統(tǒng)并不能保證多線(xiàn)程嚴(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è)需求,就要借助蘋(píng)果上的音頻播放類(lèi)在后臺(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è)面流暢性的方法,直接在主線(xiàn)程中,提高timer的runloop權(quán)限,不過(guò)建議為了用戶(hù)體驗(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]

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容