在現(xiàn)在很多app中宴胧,我們經(jīng)常會看到輪播圖漱抓,輪播廣告等等,比如淘寶恕齐、京東商城app辽旋,他們都可以定時循環(huán)地播放廣告、圖片檐迟,背后的功臣之一就是今天的主角——定時器 NSTimer。
簡單地介紹了它的應(yīng)用場景码耐,接下來追迟,說說此次要分享的技能點:
- 定時器的常用方式
- fire方法的正確理解
- NSRunloopMode對定時器的影響
- 子線程開啟定時器
- GCD定時器
- 定時器引起的循環(huán)引用的解決思路
定時開始:
我創(chuàng)建一個
HomeViewController
,然后讓他成為導(dǎo)航控制器的
RootViewController
骚腥,在HomeViewController
的viewDidLoad
調(diào)用定時器方法敦间,如下代碼:
#import "HomeViewController.h"
@interface HomeViewController ()
@end
@implementation HomeViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self regularTime];
}
/*
定時器的常規(guī)用法
*/
- (void)regularTime
{
//自動開啟
[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
}
- (void)timerAction
{
NSLog(@"定時器:%s",__func__);
}
@end
運行結(jié)果:每隔一秒就打印一次。
還有另外一種方式也可以開啟定時器束铭,那就是調(diào)用這個方法:
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(nullable id)userInfo repeats:(BOOL)yesOrNo;
注意1:單獨地寫這一句代碼廓块,默認(rèn)是不會開啟定時器的,讓我們看看蘋果官方原文是怎么說的:“
You must add the new timer to a run loop, using addTimer:forMode:. Then, after ti seconds have elapsed, the timer fires, sending the message aSelector to target. (If the timer is configured to repeat, there is no need to subsequently re-add the timer to the run loop.)
”
也就是說契沫,需要添加到runloop中带猴,手動開啟。運行的結(jié)果同上懈万。
注意2:在主線程中拴清,runloop是默認(rèn)開啟,如果是在子線程中開啟開啟定時器会通,那么我們還需要手動開啟runloop口予。運行的結(jié)果同上。
注意3: NSRunLoopCommonModes和NSDefaultRunLoopMode優(yōu)先級使用場景不同:一般都是默認(rèn)模式涕侈。
- 當(dāng)使用NSTimer的
scheduledTimerWithTimeInterval
方法時沪停,此時Timer會被加入到當(dāng)前線程的RunLoop中,且模式是默認(rèn)的NSDefaultRunLoopMode
裳涛,如果當(dāng)前線程就是主線程木张,也就是UI線程時,某些UI事件调违,比如UIScrollView的拖動操作窟哺,會將RunLoop切換成NSEventTrackingRunLoopMode
模式,在這個過程中技肩,默認(rèn)的NSDefaultRunLoopMode
模式中注冊的事件是不會被執(zhí)行的且轨,也就是說浮声,此時使用scheduledTimerWithTimeInterval
添加到RunLoop中的Timer就不會執(zhí)行。 - 所以為了設(shè)置一個不被UI干擾的Timer旋奢,使用的模式是:
NSRunLoopCommonModes
泳挥,這個模式等效于NSDefaultRunLoopMode
和NSEventTrackingRunLoopMode
的結(jié)合。(參考官方文檔)
代碼如下所示:
- (void)viewDidLoad {
[super viewDidLoad];
//在主線程中開啟定時器
[self regularTime];
//在子線程中開啟定時器
// [NSThread detachNewThreadWithBlock:^{
// NSLog(@"%@",[NSThread currentThread]);
// [self regularTime];
// }];
}
- (void)regularTime
{
timer = [NSTimer timerWithTimeInterval:1.0f target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
//runloop中添加定時器
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
//在子線程中啟用定時器必須開啟runloop
// [[NSRunLoop currentRunLoop] run];
}
- (void)timerAction
{
NSLog(@"定時器:%s",__func__);
}
fire方法
簡單說說fire方法至朗,fire是火焰的意思屉符,從字面意思可以聯(lián)想到燃料、加速的意思锹引,that’s right矗钟!
[timer fire]
——>就是加速計時的意思,我們通過比如點擊事件嫌变,來讓定時器人為地加速計時吨艇,這個比較簡單,這里就不多贅述腾啥。
GCD定時器
GCD定時器东涡,通過創(chuàng)建隊列、創(chuàng)建資源來實現(xiàn)定時的功能倘待,如下代碼所示:
注意:如果延遲2秒才開啟定時器疮跑,那么dispatch_resume(gcdTimer)
必須寫在外面。
#import "HomeViewController.h"
@interface HomeViewController ()
{
NSTimer * timer;
}
@end
@implementation HomeViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self gcdTimer:1 repeats:YES];
}
- (void)gcdTimer:(int)timerInterVal repeats:(BOOL)repeat
{
//創(chuàng)建隊列
dispatch_queue_t queue = dispatch_queue_create("my queue", 0);
//創(chuàng)建資源
dispatch_source_t gcdTimer = dispatch_source_create(&_dispatch_source_type_timer, 0, 0, queue);
dispatch_source_set_timer(gcdTimer,dispatch_time(DISPATCH_TIME_NOW, 0),1*NSEC_PER_SEC,0);
dispatch_source_set_event_handler(gcdTimer, ^{
if (repeat) {
NSLog(@"重復(fù)了");
[self timerAction];
} else
{
// [self timerAction];
// //調(diào)用這個方法凸舵,釋放定時器
// dispatch_source_cancel(gcdTimer);
//延遲兩秒會出現(xiàn)什么情況呢祖娘?
/*
為何會調(diào)用兩次?2秒之后再觸發(fā)定時器后贞间,耽擱了0.001秒去cancel贿条,那定時器已經(jīng)再次
觸發(fā)了,所以走了兩次增热,解決的方法就是把cancel寫在外面整以。
*/
dispatch_after(dispatch_time(DISPATCH_TIME_NOW,(int64_t)
(timerInterVal*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self timerAction];
});
dispatch_source_cancel(gcdTimer);
}
});
dispatch_resume(gcdTimer);
}
/*
定時器的常規(guī)用法
*/
- (void)regularTime
{
//自動開啟
[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector
(timerAction) userInfo:nil repeats:YES];
}
- (void)timerAction
{
NSLog(@"定時器:%s",__func__);
}
定時器循環(huán)引用的解決思路
- 循環(huán)引用出現(xiàn)的場景:
eg:有兩個控制器A和B费彼,A 跳轉(zhuǎn)到B中汛骂,B開啟定時器蜕劝,但是當(dāng)我返回A界面時磷斧,定時器依然還在走择膝,控制器也并沒有執(zhí)行dealloc方法銷毀掉昨凡。
- 為何會出現(xiàn)循環(huán)引用的情況呢导街?
原因是:定時器對控制器 (self) 進行了強引用耻蛇。
先說簡單的解決思路:
蘋果官方為了給我們解決這個循環(huán)引用的問題吭从,提供了一個定時器的新的自帶方法:
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)interval repeats:(BOOL)repeats block:(void (^)(NSTimer *timer))block;
代碼如下:
- (void)regularTime
{
//用蘋果自帶的方法,使用weakself就可以解決定時器循環(huán)引用問題
__weak typeof(self)weakSelf = self;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0f repeats:YES block:^(NSTimer * _Nonnull timer) {
[weakSelf timerAction];
}];
}
第二種思路:
- 既然引發(fā)循環(huán)引用是因為Timer對self的強引用朝蜘,那我讓Timer不對self強引用,不就解決了涩金。
- 本人非常興奮地實驗了兩種方法:timer=nil(失敗)谱醇、__weak typeof(self)weakself = self(失敗)暇仲,都是調(diào)用系統(tǒng)自帶的方法,如下代碼:
- (void)regularTime
{
//自動開啟
//timer置為nil或者__weak typeof(self)weakself = self也無法解決定時器循環(huán)引用問題
__weak typeof(self)weakself = self;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:weakself selector:
@selector(timerAction) userInfo:nil repeats:YES];
}
既然如此副渴,那該如何是好奈附?
答案是:通過類擴展,自己改寫NSTimer的類方法煮剧,在控制器中調(diào)用新的類方法斥滤,直接show the code:
NSTimer+HomeTimer.h中:
#import <Foundation/Foundation.h>
@interface NSTimer (HomeTimer)
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)timerInterval block:(void(^)())block repeats:(BOOL)repeat;
+ (void)timerAction:(NSTimer *)timer;
@end
NSTimer+HomeTimer.m中:
#import "NSTimer+HomeTimer.h"
@implementation NSTimer (HomeTimer)
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)timerInterval block:(void(^)())block repeats:(BOOL)repeat
{
return [self timerWithTimeInterval:timerInterval target:self selector:@selector(timerAction:) userInfo:block repeats:YES];
}
+ (void)timerAction:(NSTimer *)timer
{
void (^block)() = [timer userInfo];
if (block) {
block();
}
}
@end
類擴展寫好之后,在控制器中調(diào)用勉盅,重寫類方法佑颇,讓定時器對NSTimer類強引用,類是沒有內(nèi)存空間的草娜,就沒有循環(huán)引用漩符,跟蘋果提供的新方法是類似的處理方式,如下代碼和運行結(jié)果所示:
#import "HomeTimerViewController.h"
#import "NSTimer+HomeTimer.h"
@interface HomeTimerViewController ()
{
NSTimer * timer;
}
@end
@implementation HomeTimerViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self regularTime];
self.view.backgroundColor = [UIColor greenColor];
}
- (void)regularTime
{
__weak typeof(self)weakSelf = self;
timer = [NSTimer timerWithTimeInterval:1.0f block:^{
[weakSelf timerAction];
} repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
}
- (void)timerAction
{
NSLog(@"定時器:%s",__func__);
}
- (void)dealloc
{
NSLog(@"%s",__func__);
}
@end
定時結(jié)束:用時2小時32分鐘
總結(jié):
我之前在開發(fā)app的時候驱还,對定時器更多是會用的層次,經(jīng)過這次的深入學(xué)習(xí)凸克,對定時器的原理有了更深入的理解议蟆、認(rèn)識,技術(shù)的提升萎战,很多時候都是基礎(chǔ)知識的延伸咐容,對原理理解透徹,很多東西就可以舉一反三蚂维,全部都通了戳粒,希望對自己和各位同道人有所幫助。