1.NSTimer的介紹
(1.)8種創建方法
<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>種創建方式需要我們自己初始化一個Invocation對象,而其他幾種不需要。具體的創建方法:
NSInvocation* invo = [NSInvocationinvocation WithMethodSignature:[[self class] instanceMethodSignatureForSelector:@selector(init)]];??
[invo setTarget:self];??
[invo setSelector:@selector(myLog)];
NSTimer* timer = [NSTimertimerWith TimeInterval:1invocation:invo repeats:YES];
//加入主循環池中
[[NSRunLoop mainRunLoop]addTimer:timer forMode:NSDefaultRunLoopMode];
//開始循環
[timer fire];
2.<1>,<3>,<5>這三個timerWithTimeInterval方式創建的方法需要手動加入RunLoop,并且調用[timer fire];開始循環,如果沒有加入RunLoop或者沒有調用fire都不會執行循環;
3.<2>,<4>,<6>這三個scheduledTimerWithTimeInterval方式創建的方法不需要手動調用fire,會在到設定的循環時間自動執行,并且幫你加入RunLoop,但是如果你想循環立即執行還是需要調用fire;
4.<7>,<8>這兩個initWithFireDate方式創建的方法需要自己手動加入RunLoop不需要調用fire,會在到設定的循環時間自動執行;
(2.)NSTimer與RunLoop
RunLoop有兩種運行的model
NSDefaultRunLoopMode, 默認的mode
UITrackingRunLoopMode, 當處理UI滾動操作時的mode
通過scheduledTimerWithTimeInterval創建的timer,系統幫我們加入了RunLoop,但是unLoop的model是NSDefaultRunLoopMode,所以說當你滾定UI時,循環事件不會被執行
如果你想滾動UI,而循環事件不受影響,你需要選RunLoop的model為NSRunLoopCommonModes,NSRunLoopCommonModes可以理解為NSDefaultRunLoopMode+UITrackingRunLoopMode;
[[NSRunLoop currentRunLoop]addTimer:_timer forMode:NSRunLoopCommonModes];
(3.)成員變量以及其他方法
@property (copy) NSDate *fireDate;
這是設置定時器的啟動時間,常用來管理定時器的啟動與停止
//啟動定時器
timer.fireDate = [NSDatedistantPast];
//暫停定時器
timer.fireDate = [NSDatedistantFuture];
@property (readonly) NSTimeInterval timeInterval;
這個是定時器的循環間隔時間;
@property NSTimeInterval tolerance NS_AVAILABLE(10_9, 7_0);
因為NSTimer并不完全精準,這個值是設置NSTimer的誤差范圍
@property (readonly, getter=isValid) BOOL valid;
定時器是否有效;
@property (nullable, readonly, retain) id userInfo;
定時器的參數信息;
- (void)fire;
NSTimer立即開始執行。
- (void)invalidate;
停止NSTimer,將NSTimer從RunLoop中移除。
2.NSTimer的內存泄漏問題
我們知道NSTimer創建了就要有停止,我們通常在dealloc方法內停止釋放timer
- (void)dealloc {
NSLog(@"已經銷毀");
if (self.timer.isValid) {
[self.timer invalidate];
}
}
但是如果你是用<1>,<2>,<3>,<4>,<8>這幾種方式通過target來增加循環事件的創建方式創建的timer,這里的dealloc方法將不會被調用,會引起內存泄漏,原因是如果要讓timer運行的時候執行viewController下面的timerSelector:,timer需要知道target,并且保存這個target,以便于在以后執行這個代碼 [target performSelector:],這里的target就是viewController,這樣viewController與timer就是相互強引用,這樣就形成了retain cycle;如果timer不能被釋放retain cycle就不能被打破,viewContrller也不會被釋放,那么dealloc方法就不會走了,所以應該在viewController的viewWillDisappear方法內釋放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形式增加循環事件創建的timer可以避免timer強引用viewController這個問題,但是如果block內部涉及到self的一定要將self弱化,不然viewController依然不能被釋放。
__weak typeof (self)ws = self;
_timer = [NSTimer scheduledTimerWithTimeInterval:1.0 repeats:YES block:^(NSTimer * _Nonnull timer) {
[ws avtion];
}];
一點點自己的愚見,如果有誤歡迎大家指出,Demo下載地址?