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

iOS 中的 NSTimer

移動開發 iOS
前陣子在整理公司項目的時候,發現老代碼在使用 NSTimer 時出現了內存泄露。然后整理了一些 NSTimer 的相關內容。比較簡單,各位見笑啦。

[[139776]]

前陣子在整理公司項目的時候,發現老代碼在使用 NSTimer 時出現了內存泄露。然后整理了一些 NSTimer 的相關內容。比較簡單,各位見笑啦。

NSTimer

fire

我們先用 NSTimer 來做個簡單的計時器,每隔5秒鐘在控制臺輸出 Fire 。比較想當然的做法是這樣的:

  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 

運行之后確實在控制臺每隔3秒鐘輸出一次 Fire ,然而當我們從這個界面跳轉到其他界面的時候卻發現:控制臺還在源源不斷的輸出著 Fire 。看來 Timer 并沒有停止。

 

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

 

再次運行,還是沒有停止。原因是 Timer 添加到 Runloop 的時候,會被 Runloop 強引用:
 

  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 又會有一個對 Target 的強引用(也就是 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. 

也就是說 NSTimer 強引用了 self ,導致 self 一直不能被釋放掉,所以也就走不到 self 的 dealloc 里。

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

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

嗯這樣就可以了。(在 SOF 上有人說該在 invalidate 之后執行 _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 在哪個線程創建就要在哪個線程停止,否則會導致資源不能被正確的釋放??雌饋砀鞣N坑還不少。

dealloc

那么問題來了:如果我就是想讓這個 NSTimer 一直輸出,直到 DemoViewController 銷毀了才停止,我該如何讓它停止呢?

NSTimer 被 Runloop 強引用了,如果要釋放就要調用 invalidate 方法。

但是我想在 DemoViewController 的 dealloc 里調用 invalidate 方法,但是 self 被 NSTimer 強引用了。

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

然而你不進入到 dealloc 方法里我又不能調用 invalidate 方法。

嗯…

HWWeakTimer

weakSelf

問題的關鍵就在于 self 被 NSTimer 強引用了,如果我們能打破這個強引用問題自然而然就解決了。所以一個很簡單的想法就是: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]; 

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

target

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

  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 

然后我們再封裝個假的 scheduledTimerWithTimeInterval 方法,但是在調用的時候已經偷梁換柱了:

  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; 

再次運行,問題解決。

block

如果能用 block 來調用 NSTimer 那豈不是更好了。我們可以這樣來實現:

  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 里寫相關邏輯了:

  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

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

責任編輯:chenqingxiang 來源: CocoaChina
相關推薦

2017-05-04 20:15:51

iOSNSTimer循環引用

2013-04-28 10:53:44

iOS開發NSDate計算日期計算

2015-03-18 09:29:12

iOS開發爭議

2015-07-08 16:46:05

iOS鍵盤

2015-10-20 11:22:34

iOS開發Git

2014-05-09 11:23:29

iOS移動互聯網

2013-01-06 09:52:43

SQLite

2013-04-09 16:04:06

iOS開發SQLite知識總結

2017-01-10 13:33:51

iOS編程throttle

2013-06-14 13:50:28

iOS開發移動開發警告視圖

2014-04-23 14:40:06

iOS開發KVO內部實現

2013-07-22 13:48:55

iOS開發ASIHTTPRequ使用Cookie

2014-02-19 09:59:52

iOS開發Html解析

2011-12-01 09:25:33

iOS 5移動開發iOS

2014-03-04 15:28:32

iOS開發消息傳遞機制

2014-04-23 13:30:23

類簇iOS開發

2013-03-27 11:33:32

iOS開發iOSjson解析方式

2013-04-01 10:49:51

iOS開發sqlite數據庫

2014-06-23 10:42:56

iOS開發UIScrollVie

2014-02-26 14:24:40

iOSUIScrollVieUIview
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 亚洲免费精品 | 精品久久网 | 日韩区| 亚洲一区在线播放 | 亚洲成人精品久久久 | 免费午夜剧场 | 日韩亚洲一区二区 | 久久久久国产一区二区三区四区 | 综合久久av | 日本欧美在线 | 天天躁日日躁狠狠的躁天龙影院 | 久久精品国产一区二区电影 | 国产一区二区三区欧美 | 精品一区av| 久久国产精品视频免费看 | 欧美区在线观看 | 欧美精品在线一区二区三区 | 性大毛片视频 | 欧美激情亚洲天堂 | 中文字幕精品一区久久久久 | 久久精品国产99国产精品亚洲 | 久久国产欧美日韩精品 | 久久草在线视频 | 久热国产精品视频 | 亚洲一区在线日韩在线深爱 | 国产人成精品一区二区三 | 色网在线看 | 国产资源在线观看 | 一级黄a视频 | 欧美精品一区在线 | 免费毛片网站在线观看 | 精品久久久久久久久久久下田 | 色片在线观看 | 99久久精品免费看国产四区 | 欧美a免费 | 国产精品久久久久久久久久久久 | www.亚洲国产精品 | 91pao对白在线播放 | 成人区一区二区三区 | 蜜臀久久 | 日韩在线日韩 |