定時器NSTimer總結



timer不是一種實時的機制,會存在延遲,而且延遲的程度跟當前線程的執行情況有關



timer都會對它的target進行retain,我們需要小心對待這個target的生命週期問題,尤其是重複性的timer



感謝CSDN一位博主的分享,CSDN:enuola。原文地址:http://blog.csdn.net/enuola/article/details/8099461


IOS中定時器NSTimer的開啓與關閉

調用一次計時器方法:

myTimer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(scrollTimer) userInfo:nil repeats:NO];  
//不重複,只調用一次。timer運行一次就會自動停止運行  

重複調用計時器方法:

timer =  [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(function:) userInfo:nil repeats:YES];  
//每1秒運行一次function方法。  

注意:將計數器的repeats設置爲YES的時候,self的引用計數會加1。因此可能會導致self(即viewController)不能release,所以,必須在viewWillAppear的時候,將計數器timer停止,否則可能會導致內存泄露。

停止timer的運行,但這個是永久的停止:(注意:停止後,一定要將timer賦空,否則還是沒有釋放。)

//取消定時器  
[timer invalidate];  
timer = nil;  

要想實現:先停止,然後再某種情況下再次開啓運行timer,可以使用下面的方法:

首先關閉定時器不能使用上面的方法,應該使用下面的方法:

//關閉定時器  
[myTimer setFireDate:[NSDate distantFuture]];  

然後就可以使用下面的方法再此開啓這個timer了:

//開啓定時器  
[myTimer setFireDate:[NSDate distantPast]];

例子:比如,在頁面消失的時候關閉定時器,然後等頁面再次打開的時候,又開啓定時器。

(主要是爲了防止它在後臺運行,暫用CPU)可以使用下面的代碼實現:

//頁面將要進入前臺,開啓定時器  
-(void)viewWillAppear:(BOOL)animated  
{  
    //開啓定時器  
    [myTimer setFireDate:[NSDate distantPast]];  
}  
  
//頁面消失,進入後臺不顯示該頁面,關閉定時器  
-(void)viewDidDisappear:(BOOL)animated  
{  
    //關閉定時器  
    [myTimer setFireDate:[NSDate distantFuture]];  
} 

注意:定時器的調用,放在主線程中最優! 在gcd dispatch_async中執行可能會無效!


IOS 中NSTimer使用注意事項
原文地址:http://www.2cto.com/kf/201405/301572.html

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中.

sample:

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

NSTimer *myTimer = [NSTimertimerWithTimeInterval:3.0 target:selfselector:@selector(timerFired:) userInfo:nilrepeats:NO];

[[NSRunLoopcurrentRunLoop] addTimer:myTimerforMode:NSDefaultRunLoopMode];

2、觸發(啓動)

當定時器創建完(不用scheduled的,添加到runloop中後,該定時器將在初始化時指定的timeInterval秒後自動觸發。

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

NSRunLoop *runLoop=[NSRunLoop currentRunLoop];

[runLoop addTimer:timer forMode: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:self selector:@selector(timeSchedule) userInfo:nil repeats:YES];

NSRunLoop *runLoop=[NSRunLoop currentRunLoop];

[runLoop addTimer:_timer forMode:NSDefaultRunLoopMode];

while ([runLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]);





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章