關(guān)于NSTimer CADisplayLink GCD計(jì)時(shí)器的雜談
** NSTimer **
iOS中最常用的定時(shí)器 一般來說計(jì)時(shí)是相對準(zhǔn)確 但是如果在當(dāng)前循環(huán)中有耗時(shí)操作 下一次定時(shí)可能就會(huì)延后 (在一個(gè)循環(huán)中加入一個(gè)for循環(huán)并進(jìn)行打印操作可以明顯檢測到)另外把計(jì)時(shí)器加入的RunLoop的模式也會(huì)對他有影響 使用下面的代碼時(shí)候 當(dāng)用戶進(jìn)行UI交互操作的時(shí)候(比如滑動(dòng)tableView)主線程切換到TrackingRunLoopModel 在這種模式下定時(shí)器不會(huì)觸發(fā)
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopDefaultModel];
另外NSTimer容易造成循環(huán)引用 在下述常規(guī)使用中 就容易造成循環(huán)引用 如果沒及時(shí)調(diào)用 invalidate 方法容易造成內(nèi)存泄漏 一般都是控制器沒被銷毀的大內(nèi)存泄漏 比如一般錯(cuò)誤的寫法在dealloc 里面調(diào)用
[_timer invalidate ];
_timer = nil;
因?yàn)镹STimer 會(huì)強(qiáng)引用target NSRunLoop又會(huì)持有NSTimer 使用者(target) 同時(shí)又持有NSTimer 這就造成了循環(huán)引用
第一種誤區(qū) 我們可能會(huì)將NSTimer聲明為弱引用 但是target的釋放依賴NSTimer的釋放 而NSTimer的釋放在target的dealloc中 所以還是會(huì)循環(huán)引用
第二種誤區(qū) 將NSTimer 弱引用self 但是NSTimer 還是會(huì)強(qiáng)引用target 估計(jì)NSTimer內(nèi)部聲明了一個(gè)__strong指針
self.timer1 = [NSTimer timerWithTimeInterval:5.0 target:self selector:@selector(timerMethod1) userInfo:nil repeats:NO];
解決循環(huán)引用的辦法
第一種 使用 分類 使用的時(shí)候只需要block里面避免強(qiáng)引用造成的循環(huán)引用即可 有點(diǎn)類似iOS10.0以后系統(tǒng)提供的block的接口
+ (NSTimer *)AZ_scheduledTimerWithTimeInterval:(NSTimeInterval)inTimeInterval block:(void (^)())inBlock repeats:(BOOL)inRepeats
{
void (^block)() = [inBlock copy];
NSTimer * timer = [self scheduledTimerWithTimeInterval:inTimeInterval target:self selector:@selector(__executeTimerBlock:) userInfo:block repeats:inRepeats];
return timer;
}
+ (NSTimer *)AZ_timerWithTimeInterval:(NSTimeInterval)inTimeInterval block:(void (^)())inBlock repeats:(BOOL)inRepeats
{
void (^block)() = [inBlock copy];
NSTimer * timer = [self timerWithTimeInterval:inTimeInterval target:self selector:@selector(__executeTimerBlock:) userInfo:block repeats:inRepeats];
return timer;
}
+ (void)__executeTimerBlock:(NSTimer *)inTimer;
{
if([inTimer userInfo])
{
void (^block)() = (void (^)())[inTimer userInfo];
block();
}
}
另外 在創(chuàng)建NSTimer的時(shí)候先要將定時(shí)器停止 慎框,不然可能會(huì)出現(xiàn)僵尸定時(shí)器 哩簿,導(dǎo)致不可預(yù)估的錯(cuò)誤
第二種 GCD自己實(shí)現(xiàn)Timer YYKit里面有現(xiàn)成的類YYTimer
第三種 代理Proxy NSProxy 相當(dāng)于一個(gè)中間代理人 并將消息轉(zhuǎn)發(fā)給真正實(shí)現(xiàn)這個(gè)方法的類
使用的時(shí)候只需要將self 替換成 [YYWeakProxy proxyWithTarget:self] 借用了YYWeakProxy里面的實(shí)現(xiàn)
- (instancetype)initWithTarget:(id)target {
_target = target;
return self;
}
+ (instancetype)proxyWithTarget:(id)target {
return [[YYWeakProxy alloc] initWithTarget:target];
}
- (id)forwardingTargetForSelector:(SEL)selector {
return _target;
}
- (void)forwardInvocation:(NSInvocation *)invocation {
void *null = NULL;
[invocation setReturnValue:&null];
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector {
return [NSObject instanceMethodSignatureForSelector:@selector(init)];
}
- (BOOL)respondsToSelector:(SEL)aSelector {
return [_target respondsToSelector:aSelector];
}
- (BOOL)isEqual:(id)object {
return [_target isEqual:object];
}
- (NSUInteger)hash {
return [_target hash];
}
- (Class)superclass {
return [_target superclass];
}
- (Class)class {
return [_target class];
}
- (BOOL)isKindOfClass:(Class)aClass {
return [_target isKindOfClass:aClass];
}
- (BOOL)isMemberOfClass:(Class)aClass {
return [_target isMemberOfClass:aClass];
}
- (BOOL)conformsToProtocol:(Protocol *)aProtocol {
return [_target conformsToProtocol:aProtocol];
}
- (BOOL)isProxy {
return YES;
}
個(gè)人比較推薦第三種方式 沒有對NSTimer 進(jìn)行耦合 對原生代碼的入侵比較小 同時(shí)CADisplayLink 也可以使用上述的三種方式 解除循環(huán)引用
CADisplayLink
CADisplayLink 是一個(gè)讓我們以和屏幕刷新率相同的頻率的定時(shí)器 (利用這一點(diǎn)可以在測試APP時(shí)懸浮一個(gè)窗口在window上實(shí)時(shí)查看實(shí)時(shí)幀率) 當(dāng)CADispLink以特定的模式注冊到runloop之后 當(dāng)屏幕需要刷新的時(shí)候會(huì)調(diào)用CADisplayLink綁定的target的selector 并且這個(gè)時(shí)候可以讀取到這次調(diào)用的時(shí)間戳
CADisplay適合UI不停重繪的場合 比如剛說的實(shí)時(shí)顯示當(dāng)前屏幕刷新率的label FLAnimatedImageView YYAnimatedImageView 里面的加載gif圖 文字閃爍漸入漸出的動(dòng)畫效果的RQShineLabel
GCDTimer
GCDTimer不受當(dāng)前RunLoopMode的影響 但是計(jì)時(shí)也不是絕對準(zhǔn)確的 當(dāng)GCD內(nèi)部管理的所有線程都被占用的時(shí)候 其觸發(fā)的事件將被延遲(這種很少見 幾乎不可能)
__block NSInteger remain = showtime*20; //倒計(jì)時(shí)時(shí)間
__block NSInteger count = 0;
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), 0.05 * NSEC_PER_SEC, 0); // 每秒執(zhí)行
@weakify(self);
dispatch_source_set_event_handler(_timer, ^{
dispatch_async(dispatch_get_main_queue(), ^{
if (count >= remain) {
dispatch_source_cancel(_timer);
weak_self.viewLayer.strokeStart = 1;
[weak_self dismiss]; // 關(guān)閉界面
} else {
weak_self.viewLayer.strokeStart += 0.01;
count++; //剩余時(shí)間進(jìn)行自加
}
});
});
dispatch_resume(_timer);
注意計(jì)時(shí)器 不一定是在主線程進(jìn)行 所以UI相關(guān)的操作要放到主線程進(jìn)行 timer對象要被持有 如果沒被持有 定會(huì)器銷毀 timer就不會(huì)正常運(yùn)行