1.NSTimer的介紹
(1.)8種創(chuàng)建方法
<1>? + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo;
<2>? + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo;
<3>? + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(nullable id)userInfo repeats:(BOOL)yesOrNo;
<4>? + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(nullable id)userInfo repeats:(BOOL)yesOrNo;
<5>? + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)interval repeats:(BOOL)repeats block:(void (^)(NSTimer *timer))block;
<6>? + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)interval repeats:(BOOL)repeats block:(void (^)(NSTimer *timer))block
<7>? - (instancetype)initWithFireDate:(NSDate *)date interval:(NSTimeInterval)interval repeats:(BOOL)repeats block:(void (^)(NSTimer *timer))block ;
<8>? - (instancetype)initWithFireDate:(NSDate *)date interval:(NSTimeInterval)ti target:(id)t selector:(SEL)s userInfo:(nullable id)ui repeats:(BOOL)rep;
1.第<1>,<2>種創(chuàng)建方式需要我們自己初始化一個(gè)Invocation對(duì)象咧最,而其他幾種不需要司浪。具體的創(chuàng)建方法:
NSInvocation* invo = [NSInvocationinvocation WithMethodSignature:[[self class] instanceMethodSignatureForSelector:@selector(init)]];??
[invo setTarget:self];??
[invo setSelector:@selector(myLog)];
NSTimer* timer = [NSTimertimerWith TimeInterval:1invocation:invo repeats:YES];
//加入主循環(huán)池中
[[NSRunLoop mainRunLoop]addTimer:timer forMode:NSDefaultRunLoopMode];
//開始循環(huán)
[timer fire];
2.<1>,<3>,<5>這三個(gè)timerWithTimeInterval方式創(chuàng)建的方法需要手動(dòng)加入RunLoop癣猾,并且調(diào)用[timer fire];開始循環(huán)俗他,如果沒有加入RunLoop或者沒有調(diào)用fire都不會(huì)執(zhí)行循環(huán);
3.<2>,<4>,<6>這三個(gè)scheduledTimerWithTimeInterval方式創(chuàng)建的方法不需要手動(dòng)調(diào)用fire筹误,會(huì)在到設(shè)定的循環(huán)時(shí)間自動(dòng)執(zhí)行思犁,并且?guī)湍慵尤隦unLoop驻子,但是如果你想循環(huán)立即執(zhí)行還是需要調(diào)用fire;
4.<7>,<8>這兩個(gè)initWithFireDate方式創(chuàng)建的方法需要自己手動(dòng)加入RunLoop不需要調(diào)用fire牡彻,會(huì)在到設(shè)定的循環(huán)時(shí)間自動(dòng)執(zhí)行扫沼;
(2.)NSTimer與RunLoop
RunLoop有兩種運(yùn)行的model
NSDefaultRunLoopMode, 默認(rèn)的mode
UITrackingRunLoopMode, 當(dāng)處理UI滾動(dòng)操作時(shí)的mode
通過scheduledTimerWithTimeInterval創(chuàng)建的timer,系統(tǒng)幫我們加入了RunLoop讨便,但是unLoop的model是NSDefaultRunLoopMode充甚,所以說當(dāng)你滾定UI時(shí),循環(huán)事件不會(huì)被執(zhí)行
如果你想滾動(dòng)UI霸褒,而循環(huán)事件不受影響伴找,你需要選RunLoop的model為NSRunLoopCommonModes,NSRunLoopCommonModes可以理解為NSDefaultRunLoopMode+UITrackingRunLoopMode;
[[NSRunLoop currentRunLoop]addTimer:_timer forMode:NSRunLoopCommonModes];
(3.)成員變量以及其他方法
@property (copy) NSDate *fireDate;
這是設(shè)置定時(shí)器的啟動(dòng)時(shí)間废菱,常用來管理定時(shí)器的啟動(dòng)與停止
//啟動(dòng)定時(shí)器
timer.fireDate = [NSDatedistantPast];
//暫停定時(shí)器
timer.fireDate = [NSDatedistantFuture];
@property (readonly) NSTimeInterval timeInterval;
這個(gè)是定時(shí)器的循環(huán)間隔時(shí)間技矮;
@property NSTimeInterval tolerance NS_AVAILABLE(10_9, 7_0);
因?yàn)镹STimer并不完全精準(zhǔn),這個(gè)值是設(shè)置NSTimer的誤差范圍
@property (readonly, getter=isValid) BOOL valid;
定時(shí)器是否有效殊轴;
@property (nullable, readonly, retain) id userInfo;
定時(shí)器的參數(shù)信息衰倦;
- (void)fire;
NSTimer立即開始執(zhí)行。
- (void)invalidate;
停止NSTimer旁理,將NSTimer從RunLoop中移除樊零。
2.NSTimer的內(nèi)存泄漏問題
我們知道NSTimer創(chuàng)建了就要有停止,我們通常在dealloc方法內(nèi)停止釋放timer
- (void)dealloc {
NSLog(@"已經(jīng)銷毀");
if (self.timer.isValid) {
[self.timer invalidate];
}
}
但是如果你是用<1>,<2>,<3>,<4>,<8>這幾種方式通過target來增加循環(huán)事件的創(chuàng)建方式創(chuàng)建的timer,這里的dealloc方法將不會(huì)被調(diào)用驻襟,會(huì)引起內(nèi)存泄漏夺艰,原因是如果要讓timer運(yùn)行的時(shí)候執(zhí)行viewController下面的timerSelector:,timer需要知道target沉衣,并且保存這個(gè)target郁副,以便于在以后執(zhí)行這個(gè)代碼 [target performSelector:],這里的target就是viewController,這樣viewController與timer就是相互強(qiáng)引用豌习,這樣就形成了retain cycle存谎;如果timer不能被釋放retain cycle就不能被打破,viewContrller也不會(huì)被釋放肥隆,那么dealloc方法就不會(huì)走了既荚,所以應(yīng)該在viewController的viewWillDisappear方法內(nèi)釋放timer,這樣retain cycle被打破巷屿,viewContrller也就能被釋放了固以。
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
if (self.timer.isValid) {
[self.timer invalidate];
self.timer = nil;
}
}
如果你是通過<5>,<6>,<7>通過block形式增加循環(huán)事件創(chuàng)建的timer可以避免timer強(qiáng)引用viewController這個(gè)問題,但是如果block內(nèi)部涉及到self的一定要將self弱化嘱巾,不然viewController依然不能被釋放憨琳。
__weak typeof (self)ws = self;
_timer = [NSTimer scheduledTimerWithTimeInterval:1.0 repeats:YES block:^(NSTimer * _Nonnull timer) {
[ws avtion];
}];
一點(diǎn)點(diǎn)自己的愚見,如果有誤歡迎大家指出旬昭,Demo下載地址?