對(duì)于NSTimer使用大家肯定都不陌生,貼一張使用代碼
? ? ? ? 看代碼中SecondViewController有一個(gè)屬性是testTimer茧吊,然后在[self test01]中給self.testTimer賦值贞岭,并進(jìn)行了強(qiáng)引用,然后我們?cè)诳捶椒╗NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(doSomeThing) userInfo:nil repeats:YES]搓侄,里面的target:所傳參數(shù)為self瞄桨,SecondViewController,即所以SecondViewController與self.testTimer之間存在循環(huán)引用讶踪,會(huì)造成內(nèi)存泄漏讲婚。
解決辦法:
? ? ? ? 我們發(fā)現(xiàn)testTimer是用strong修飾的,想到之前我們解決delegate循環(huán)引用是俊柔,對(duì)delegate屬性使用weak修飾來(lái)打破閉環(huán)解決循環(huán)引用問(wèn)題筹麸,那么這里testTime也使用weak修飾是否能夠解決呢?經(jīng)過(guò)測(cè)試發(fā)現(xiàn)無(wú)法解決雏婶,那問(wèn)題出現(xiàn)在哪里物赶?
先看這兩個(gè)創(chuàng)建NSTimer對(duì)象的方法:
1. + (NSTimer*)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;//生成timer但不執(zhí)行
2. + (NSTimer*)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;//生成timer并且納入當(dāng)前線程的run loop來(lái)執(zhí)行,不需要用addTimer方法留晚。
分析:
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(doSomeThing) userInfo:nil repeats:YES];
? ? //相當(dāng)于
? ? self.timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(doSomeThing) userInfo:nil repeats:YES];
? ? [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];? ? ? 開(kāi)啟一個(gè)NSTimer實(shí)質(zhì)上是在當(dāng)前的runloop中注冊(cè)了一個(gè)新的事件源, 所以會(huì)發(fā)現(xiàn)NSRunLoop也對(duì)timer進(jìn)行了強(qiáng)引用酵紫。看下面示意圖(假設(shè)testTimer屬性用weak修飾)
所以timer引用計(jì)數(shù)永遠(yuǎn)也不會(huì)變成0错维,那么也就不會(huì)銷毀奖地,這樣也就導(dǎo)致了對(duì)SecondViewController的引用也不會(huì)斷開(kāi),那么SecondViewController也不會(huì)銷毀了赋焕。
蘋果給NSTimer類提供的有一個(gè)對(duì)象方法
- (void)invalidate; //銷毀定時(shí)器的方法参歹,詳解:將timer從runloop上移除的唯一方法,同時(shí)timer也會(huì)移除target目標(biāo)和userinfo參數(shù)對(duì)象的強(qiáng)引用關(guān)系
Stops the timer from ever firing again and requests its removal from its run loop.
This method is the only way to remove a timer from an NSRunLoop object. The NSRunLoop object removes its strong reference to the timer, either just before the invalidate method returns or at some later point.
If it was configured with target and user info objects, the receiver removes its strong references to those objects as well.
問(wèn)題: 那么定時(shí)器timer屬性是用strong修飾還是用weak修飾隆判?
答:都可以犬庇,如果用strong修飾的話,調(diào)用- (void)invalidate方法銷毀定時(shí)器之后侨嘀,還有調(diào)用self.timer = nil來(lái)移除控制器對(duì)定時(shí)器的強(qiáng)引用臭挽,如果使用weak就不需要了。建議使用strong咬腕,strong的性能還是要更優(yōu)于weak欢峰。