你好,歡迎來到IOS教程網

 Ios教程網 >> IOS基礎知識 >> IOS基礎知識 >> iOS基礎之路:NSTimer

iOS基礎之路:NSTimer

編輯:IOS基礎知識

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:timerforMode: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只有在調用[NSRunloopcurrentRunloop]才會創建。

三,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];

}

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved