成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

iOS 中的 NSTimer

移動(dòng)開(kāi)發(fā) iOS
前陣子在整理公司項(xiàng)目的時(shí)候,發(fā)現(xiàn)老代碼在使用 NSTimer 時(shí)出現(xiàn)了內(nèi)存泄露。然后整理了一些 NSTimer 的相關(guān)內(nèi)容。比較簡(jiǎn)單,各位見(jiàn)笑啦。

[[139776]]

前陣子在整理公司項(xiàng)目的時(shí)候,發(fā)現(xiàn)老代碼在使用 NSTimer 時(shí)出現(xiàn)了內(nèi)存泄露。然后整理了一些 NSTimer 的相關(guān)內(nèi)容。比較簡(jiǎn)單,各位見(jiàn)笑啦。

NSTimer

fire

我們先用 NSTimer 來(lái)做個(gè)簡(jiǎn)單的計(jì)時(shí)器,每隔5秒鐘在控制臺(tái)輸出 Fire 。比較想當(dāng)然的做法是這樣的:

  1. @interface DetailViewController () 
  2. @property (nonatomic, weak) NSTimer *timer; 
  3. @end 
  4. @implementation DetailViewController 
  5. - (IBAction)fireButtonPressed:(id)sender { 
  6. _timer = [NSTimer scheduledTimerWithTimeInterval:3.0f 
  7. target:self 
  8. selector:@selector(timerFire:) 
  9. userInfo:nil 
  10. repeats:YES]; 
  11. [_timer fire]; 
  12. -(void)timerFire:(id)userinfo { 
  13. NSLog(@"Fire"); 
  14. @end 

運(yùn)行之后確實(shí)在控制臺(tái)每隔3秒鐘輸出一次 Fire ,然而當(dāng)我們從這個(gè)界面跳轉(zhuǎn)到其他界面的時(shí)候卻發(fā)現(xiàn):控制臺(tái)還在源源不斷的輸出著 Fire 。看來(lái) Timer 并沒(méi)有停止。

 

  1. invalidate 
  2.  
  3. 既然沒(méi)有停止,那我們?cè)?nbsp;DemoViewController 的 dealloc 里加上 invalidate 的方法: 
  4.  
  5. -(void)dealloc { 
  6. [_timer invalidate]; 
  7. NSLog(@"%@ dealloc", NSStringFromClass([self class])); 

 

再次運(yùn)行,還是沒(méi)有停止。原因是 Timer 添加到 Runloop 的時(shí)候,會(huì)被 Runloop 強(qiáng)引用:
 

  1. Note in particular that run loops maintain strong references to their timers, so you don’t have to maintain your own strong reference to a timer after you have added it to a run loop. 

然后 Timer 又會(huì)有一個(gè)對(duì) Target 的強(qiáng)引用(也就是 self ):

  1. Target is the object to which to send the message specified by aSelector when the timer fires. The timer maintains a strong reference to target until it (the timer) is invalidated. 

也就是說(shuō) NSTimer 強(qiáng)引用了 self ,導(dǎo)致 self 一直不能被釋放掉,所以也就走不到 self 的 dealloc 里。

既然如此,那我們可以再加個(gè) invalidate 按鈕:

  1. - (IBAction)invalidateButtonPressed:(id)sender { 
  2. [_timer invalidate]; 

嗯這樣就可以了。(在 SOF 上有人說(shuō)該在 invalidate 之后執(zhí)行 _timer = nil ,未能理解為什么,如果你知道原因可以告訴我:)

在 invalidate 方法的文檔里還有這這樣一段話:

 

  1. You must send this message from the thread on which the timer was installed. If you send this message from another thread, the input source associated with the timer may not be removed from its run loop, which could prevent the thread from exiting properly. 

NSTimer 在哪個(gè)線程創(chuàng)建就要在哪個(gè)線程停止,否則會(huì)導(dǎo)致資源不能被正確的釋放。看起來(lái)各種坑還不少。

dealloc

那么問(wèn)題來(lái)了:如果我就是想讓這個(gè) NSTimer 一直輸出,直到 DemoViewController 銷(xiāo)毀了才停止,我該如何讓它停止呢?

NSTimer 被 Runloop 強(qiáng)引用了,如果要釋放就要調(diào)用 invalidate 方法。

但是我想在 DemoViewController 的 dealloc 里調(diào)用 invalidate 方法,但是 self 被 NSTimer 強(qiáng)引用了。

所以我還是要釋放 NSTimer 先,然而不調(diào)用 invalidate 方法就不能釋放它。

然而你不進(jìn)入到 dealloc 方法里我又不能調(diào)用 invalidate 方法。

嗯…

HWWeakTimer

weakSelf

問(wèn)題的關(guān)鍵就在于 self 被 NSTimer 強(qiáng)引用了,如果我們能打破這個(gè)強(qiáng)引用問(wèn)題自然而然就解決了。所以一個(gè)很簡(jiǎn)單的想法就是:weakSelf:

  1. __weak typeof(self) weakSelf = self; 
  2. _timer = [NSTimer scheduledTimerWithTimeInterval:3.0f 
  3. target:weakSelf 
  4. selector:@selector(timerFire:) 
  5. userInfo:nil 
  6. repeats:YES]; 

然而這并沒(méi)有什么卵用,這里的 __weak 和 __strong ***的區(qū)別就是:如果在這兩行代碼執(zhí)行的期間 self 被釋放了, NSTimer 的 target 會(huì)變成 nil 。

target

既然沒(méi)辦法通過(guò) __weak 把 self 抽離出來(lái),我們可以造個(gè)假的 target 給 NSTimer 。這個(gè)假的 target 類(lèi)似于一個(gè)中間的代理人,它做的***的工作就是挺身而出接下了 NSTimer 的強(qiáng)引用。類(lèi)聲明如下:

  1. @interface HWWeakTimerTarget : NSObject 
  2. @property (nonatomic, weak) id target; 
  3. @property (nonatomic, assign) SEL selector; 
  4. @property (nonatomic, weak) NSTimer* timer; 
  5. @end 
  6. @implementation HWWeakTimerTarget 
  7. - (void) fire:(NSTimer *)timer { 
  8. if(self.target) { 
  9. [self.target performSelector:self.selector withObject:timer.userInfo]; 
  10. else { 
  11. [self.timer invalidate]; 
  12. @end 

然后我們?cè)俜庋b個(gè)假的 scheduledTimerWithTimeInterval 方法,但是在調(diào)用的時(shí)候已經(jīng)偷梁換柱了:

  1. + (NSTimer *) scheduledTimerWithTimeInterval:(NSTimeInterval)interval 
  2. target:(id)aTarget 
  3. selector:(SEL)aSelector 
  4. userInfo:(id)userInfo 
  5. repeats:(BOOL)repeats { 
  6. HWWeakTimerTarget* timerTarget = [[HWWeakTimerTarget alloc] init]; 
  7. timerTarget.target = aTarget; 
  8. timerTarget.selector = aSelector; 
  9. timerTarget.timer = [NSTimer scheduledTimerWithTimeInterval:interval 
  10. target:timerTarget 
  11. selector:@selector(fire:) 
  12. userInfo:userInfo 
  13. repeats:repeats]; 
  14. return timerTarget.timer; 

再次運(yùn)行,問(wèn)題解決。

block

如果能用 block 來(lái)調(diào)用 NSTimer 那豈不是更好了。我們可以這樣來(lái)實(shí)現(xiàn):

  1. + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)interval 
  2. block:(HWTimerHandler)block 
  3. userInfo:(id)userInfo 
  4. repeats:(BOOL)repeats { 
  5. return [self scheduledTimerWithTimeInterval:interval 
  6. target:self 
  7. selector:@selector(_timerBlockInvoke:) 
  8. userInfo:@[[block copy], userInfo] 
  9. repeats:repeats]; 
  10. + (void)_timerBlockInvoke:(NSArray*)userInfo { 
  11. HWTimerHandler block = userInfo[0]; 
  12. id info = userInfo[1]; 
  13. // or `!block ?: block();` @sunnyxx 
  14. if (block) { 
  15. block(info); 

這樣我們就可以直接在 block 里寫(xiě)相關(guān)邏輯了:

  1. - (IBAction)fireButtonPressed:(id)sender { 
  2. _timer = [HWWeakTimer scheduledTimerWithTimeInterval:3.0f block:^(id userInfo) { 
  3. NSLog(@"%@", userInfo); 
  4. } userInfo:@"Fire" repeats:YES]; 
  5. [_timer fire]; 

嗯就是這樣。

More

把上面的的代碼簡(jiǎn)單的封裝到了 HWWeakTimer 中,歡迎試用。

責(zé)任編輯:chenqingxiang 來(lái)源: CocoaChina
相關(guān)推薦

2017-05-04 20:15:51

iOSNSTimer循環(huán)引用

2013-04-28 10:53:44

iOS開(kāi)發(fā)NSDate計(jì)算日期計(jì)算

2015-03-18 09:29:12

iOS開(kāi)發(fā)爭(zhēng)議

2015-07-08 16:46:05

iOS鍵盤(pán)

2015-10-20 11:22:34

iOS開(kāi)發(fā)Git

2014-05-09 11:23:29

iOS移動(dòng)互聯(lián)網(wǎng)

2013-01-06 09:52:43

SQLite

2013-04-09 16:04:06

iOS開(kāi)發(fā)SQLite知識(shí)總結(jié)

2017-01-10 13:33:51

iOS編程throttle

2013-06-14 13:50:28

iOS開(kāi)發(fā)移動(dòng)開(kāi)發(fā)警告視圖

2014-04-23 14:40:06

iOS開(kāi)發(fā)KVO內(nèi)部實(shí)現(xiàn)

2013-07-22 13:48:55

iOS開(kāi)發(fā)ASIHTTPRequ使用Cookie

2014-02-19 09:59:52

iOS開(kāi)發(fā)Html解析

2011-12-01 09:25:33

iOS 5移動(dòng)開(kāi)發(fā)iOS

2014-03-04 15:28:32

iOS開(kāi)發(fā)消息傳遞機(jī)制

2014-04-23 13:30:23

類(lèi)簇iOS開(kāi)發(fā)

2013-03-27 11:33:32

iOS開(kāi)發(fā)iOSjson解析方式

2013-04-01 10:49:51

iOS開(kāi)發(fā)sqlite數(shù)據(jù)庫(kù)

2014-06-23 10:42:56

iOS開(kāi)發(fā)UIScrollVie

2014-02-26 14:24:40

iOSUIScrollVieUIview
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: 高清国产一区二区 | 欧美亚洲高清 | 国产精品一区二区三区久久久 | 国产精品一区二区在线 | 懂色av一区二区三区在线播放 | 欧美午夜激情在线 | 日本a级大片 | 日韩在线欧美 | 国产精品99久久久久久久vr | av在线电影网站 | 国产精品伦一区二区三级视频 | 亚洲福利网 | 欧美a免费| 日韩欧美在线免费观看视频 | 久久国产精品亚洲 | 狠狠插狠狠操 | julia中文字幕久久一区二区 | 自拍偷拍亚洲视频 | 中文字幕精品一区 | 国产探花在线精品一区二区 | 日日摸天天添天天添破 | 久久综合九九 | 午夜免费网站 | 国产精品成人国产乱一区 | 成人自拍av | h视频在线观看免费 | 免费观看毛片 | 久久精品亚洲欧美日韩精品中文字幕 | av网址在线播放 | 久久精彩视频 | 亚洲国产高清高潮精品美女 | 国产精品久久一区 | 日韩精品一区二区三区在线播放 | 日韩欧美中文字幕在线视频 | 亚洲一卡二卡 | 日韩精品一区二区三区在线观看 | 日韩国产精品一区二区三区 | 欧洲一区二区视频 | 日韩乱码一二三 | 国产精品极品美女在线观看免费 | 网址黄 |