欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS NSTimer循環(huán)引用的辦法

 更新時間:2017年12月15日 09:10:20   作者:LinXunFeng  
這篇文章主要介紹了iOS NSTimer循環(huán)引用的辦法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

在當(dāng)前控制器(ViewController)的view上添加了一個自定義的view(LXFTimerView), LXFTimerView在成功創(chuàng)建出來后添加了定時器NSTimer并加入RunLoop開始工作, 當(dāng)在當(dāng)前控制器里將LXFTimerView移除掉后,定時器還在工作,而且LXFTimerView里的dealloc并沒有調(diào)用

代碼

LXFTimerView.m

#import "LXFTimerView.h"
@interface LXFTimerView()
/** 定時器 */
@property(nonatomic, weak) NSTimer *timer;
@end

@implementation LXFTimerView
- (instancetype)initWithFrame:(CGRect)frame {
  if (self = [super initWithFrame:frame]) {
    [self addTimer];
  }
  return self;
}

- (void)dealloc {
  NSLog(@"LXFTimerView - dealloc");
  [self removeTimer];
}

#pragma mark - 定時器方法
/** 添加定時器方法 */
- (void)addTimer {
  // 創(chuàng)建定時器
  if (self.timer) { return; }
  self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(log) userInfo:nil repeats:YES];
  [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}
/** 移除定時器 */
- (void)removeTimer {
  [self.timer invalidate];
  self.timer = nil;
}
- (void)log {
  NSLog(@"定時器 -- %s", __func__);
}
@end

ViewController.m

#import "ViewController.h"
#import "LXFTimerView.h"
@interface ViewController ()
/** timerView */
@property(nonatomic, weak) LXFTimerView *timerView;
@end

@implementation ViewController
- (void)viewDidLoad {
  [super viewDidLoad];
  LXFTimerView *timerView = [[LXFTimerView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 200)];
  timerView.backgroundColor = [UIColor orangeColor];
  self.timerView = timerView;
  [self.view addSubview:timerView];  
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
  [self.timerView removeFromSuperview];
}
@end

引用關(guān)系

問題就出在LXFTimerView與NSTimer之間,在創(chuàng)建定時器時執(zhí)行

[NSTimer scheduledTimerWithTimeInterval: target: selector: userInfo: repeats:];

會將LXFTimerView進(jìn)行強(qiáng)引用,什么?我怎么知道?看下圖

翻譯:定時器保持著對target的強(qiáng)引用,直到定時器作廢 那為什么LXFTimerView中的timer屬性要用weak?? 不用著急,下面即將揭曉~

解決方案

讓定時器指著另一個對象,讓那個對象來執(zhí)行LXFTimerView中需要執(zhí)行的方法。 引用關(guān)系如下圖所示

創(chuàng)建一個繼承于NSObject的類 LXFWeakTarget,并提供一個創(chuàng)建定時器的方法(蘋果官方的方法,對scheduledTimerWithTimeInterval進(jìn)行轉(zhuǎn)到定義操作【就是command+左鍵】就可以得到) LXFWeakTarget.h

#import <Foundation/Foundation.h>
@interface LXFWeakTarget : NSObject
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(nullable id)userInfo repeats:(BOOL)yesOrNo;
@end
#import "LXFWeakTarget.h"

@interface LXFWeakTarget()
@property(nonatomic, weak) id target;
@property(nonatomic, assign) SEL selector;
@end

@implementation LXFWeakTarget
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(nullable id)userInfo repeats:(BOOL)yesOrNo {
  // 創(chuàng)建當(dāng)前類的對象
  LXFWeakTarget *object = [[LXFWeakTarget alloc] init];
  object.target = aTarget;
  object.selector = aSelector;

  return [NSTimer scheduledTimerWithTimeInterval:ti target:object selector:@selector(execute:) userInfo:userInfo repeats:yesOrNo];
}
- (void)execute:(id)obj {
  [self.target performSelector:self.selector withObject:obj]; 
}
@end

在LXFTimerView.m中導(dǎo)入LXFWeakTarget的頭文件

#import "LXFWeakTarget.h"

將創(chuàng)建定時器的類改為 LXFWeakTarget

復(fù)制代碼 代碼如下:

self.timer = [LXFWeakTarget scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(log) userInfo:nil repeats:YES];

現(xiàn)在再來執(zhí)行一下程序

 

最后縷下思路

  1. 我們用一個LXFWeakTarget來替LXFTimerView執(zhí)行一些操作。
  2. 當(dāng)沒有被定時器強(qiáng)引用的LXFTimerView從父控件上被移除時,就會執(zhí)行dealloc方法,LXFTimerView被銷毀。
  3. 將定時器作廢并設(shè)為nil,這樣定時器對LXFWeakTarget的引用也沒有了,LXFWeakTarget也會被銷毀。

好,那“為什么LXFTimerView中的timer屬性要用weak”這個問題就不用多加解析了吧。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • iOS中Runtime的幾種基本用法記錄

    iOS中Runtime的幾種基本用法記錄

    RunTime顧名思義運行時,就是系統(tǒng)在運行的時候的一些機(jī)制,最主要的是消息機(jī)制。下面這篇文章主要給大家介紹了關(guān)于iOS中Runtime的幾種基本用法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-07-07
  • iOS沙盒視頻縮略圖及保存本地代碼

    iOS沙盒視頻縮略圖及保存本地代碼

    這篇文章主要為大家詳細(xì)介紹了iOS沙盒視頻縮略圖及保存本地的代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • iOS封裝倒計時按鈕HLCountDownButton示例詳解

    iOS封裝倒計時按鈕HLCountDownButton示例詳解

    這篇文章主要為大家介紹了iOS封裝倒計時按鈕HLCountDownButton示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07
  • IOSdrawRect實現(xiàn)雪花飄落效果

    IOSdrawRect實現(xiàn)雪花飄落效果

    這篇文章主要為大家詳細(xì)介紹了IOSdrawRect實現(xiàn)雪花飄落效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • ios基于UICollectionView實現(xiàn)橫向瀑布流

    ios基于UICollectionView實現(xiàn)橫向瀑布流

    這篇文章主要為大家詳細(xì)介紹了ios基于UICollectionView實現(xiàn)橫向瀑布流,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • iOS開發(fā)之tableView實現(xiàn)左滑刪除功能

    iOS開發(fā)之tableView實現(xiàn)左滑刪除功能

    我們在使用一些應(yīng)用的時候,在滑動一些聯(lián)系人的某一行的時候,會出現(xiàn)刪除、置頂、更多等等的按鈕,下面這篇文章主要就介紹了iOS用tableView實現(xiàn)左劃刪除功能的方法,有需要的朋友們可以參考借鑒,下面來一起看看吧。
    2017-01-01
  • IOS自定義UIView

    IOS自定義UIView

    本文主要介紹下存代碼的自定義UIView和能夠在storeboard中實時顯示效果的自定義UIView。下面跟著小編一起來看下吧
    2017-03-03
  • iOS實現(xiàn)帶遮罩的彈出選項卡

    iOS實現(xiàn)帶遮罩的彈出選項卡

    這篇文章主要為大家詳細(xì)介紹了iOS實現(xiàn)彈出選項卡,并附帶遮罩,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • IOS 開發(fā)之查看大圖的實現(xiàn)代碼

    IOS 開發(fā)之查看大圖的實現(xiàn)代碼

    這篇文章主要介紹了IOS 開發(fā)之查看大圖的實現(xiàn)代碼的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下
    2017-10-10
  • iOS開發(fā)項目- 基于WebSocket的聊天通訊(2)

    iOS開發(fā)項目- 基于WebSocket的聊天通訊(2)

    這篇文章主要介紹了iOS開發(fā)項目- 基于WebSocket的聊天通訊,可以實現(xiàn)錄音和音樂播放,有需要的可以了解一下。
    2016-11-11

最新評論