iOS 如何解决 NSTimer 循环引用

491 阅读4分钟

1. 前言

在使用 NSTimer,如果使用不得当特别会引起循环引用,造成内存泄露。所以怎么避免循环引用问题,下面我提出几种解决 NSTimer 的几种循环引用。

2. 原因

当你在 ViewController (简称 VC )中使用 timer 属性,由于 VC 强引用 timer,timer 的target 又是 VC 造成循环引用。当你在 VC 的 dealloc 方法中销毁 timer,发现 VC 被 pop,VC 的 dealloc 方法没走,VC 在等 timer 释放才走 dealloc,timer 释放在 dealloc 中,所以引起循环引用。

3. 解决方案

  • 在 ViewController 执行 dealloc 前释放 timer(不推荐)
  • 对定时器 NSTimer 封装
  • 苹果 API 接口解决方案(iOS 10.0 以上可用)
  • 使用 block 进行解决
  • 使用 NSProxy 进行解决

4. 在 ViewController 执行 dealloc 前释放 timer(不推荐)

  • 可以在 viewWillAppear 中创建 timer

  • 可以在 viewWillDisappear 中销毁 timer

5. 对定时器 NSTimer 封装到 PFTimer 中

代码如下:

//PFTimer.h文件#import <Foundation/Foundation.h>@interface PFTimer : NSObject//开启定时器- (void)startTimer;//暂停定时器- (void)stopTimer;@end

在 PFTimer.m 文件中代码如下:

#import "PFTimer.h"
@implementation PFTimer {
    NSTimer *_timer;}
- (void)stopTimer{
    if (_timer == nil) {        return;    }    [_timer invalidate];    _timer = nil;}

- (void)startTimer{
    _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(work) userInfo:nil repeats:YES];}
- (void)work{
    NSLog(@"正在计时中。。。。。。");}
- (void)dealloc{
   NSLog(@"%s",__func__);    [_timer invalidate];    _timer = nil;}
@end

在 ViewController 中使用代码如下:

#import "ViewController1.h"#import "PFTimer.h"
@interface ViewController1 ()
@property (nonatomic, strong) PFTimer *timer;
@end
@implementation ViewController1
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];}
- (void)viewDidLoad {    [super viewDidLoad];    self.title = @"VC1";    self.view.backgroundColor = [UIColor whiteColor];
    //自定义timer    PFTimer *timer = [[PFTimer alloc] init];    self.timer = timer;    [timer startTimer];}
- (void)dealloc {
    [self.timer stopTimer];    NSLog(@"%s",__func__);} 

运行打印结果:

-[ViewController1 dealloc]-[PFTimer dealloc] 

这个方式主要就是让 PFTimer 强引用 NSTimer,NSTimer 强引用 PFTimer,避免让NSTimer 强引用 ViewController,这样就不会引起循环引用,然后在 dealloc 方法中执行 NSTimer 的销毁,相对的 PFTimer 也会进行销毁了。

6. 苹果系统API可以解决(iOS10以上)

在 iOS 10.0 以后,苹果官方新增了关于 NSTimer 的三个 API:

+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)interval repeats:(BOOL)repeats block:(void (^)(NSTimer *timer))block API_AVAILABLE(macosx(10.12), ios(10.0), watchos(3.0), tvos(10.0));
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)interval repeats:(BOOL)repeats block:(void (^)(NSTimer *timer))block API_AVAILABLE(macosx(10.12), ios(10.0), watchos(3.0), tvos(10.0));
- (instancetype)initWithFireDate:(NSDate *)date interval:(NSTimeInterval)interval repeats:(BOOL)repeats block:(void (^)(NSTimer *timer))block API_AVAILABLE(macosx(10.12), ios(10.0), watchos(3.0), tvos(10.0)); 

这三个方法都有一个 Block 的回调方法。关于 block 参数,官方文档有说明:

the timer itself is passed as the parameter to this block when executed to aid in avoiding cyclical references。 

翻译过来就是说,定时器在执行时,将自身作为参数传递给 block,来帮助避免循环引用。使用很简单,但是要注意两点

ps;iOS开发交流技术群:欢迎你的加入,不管你是大牛还是小白都欢迎入驻 ,分享BAT,阿里面试题、面试经验,讨论技术, 大家一起交流学习成长

  1. 避免 block 的循环引用,使用 __weak 和 __strong 来避免

  2. 在持用 NSTimer 对象的类的方法中 -(void)dealloc 调用 NSTimer 的- (void)invalidate 方法;

    7. 使用 block 来解决

通过创建一个 NSTimer 的 category 名字为 PFSafeTimer,在 NSTimer+PFSafeTimer.h 代码如下:

#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface NSTimer (PFSafeTimer)
+ (NSTimer *)PF_ScheduledTimerWithTimeInterval:(NSTimeInterval)timeInterval block:(void(^)(void))block repeats:(BOOL)repeats;
@end
NS_ASSUME_NONNULL_END 

在 NSTimer+PFSafeTimer.m 中的代码如下:

#import "NSTimer+PFSafeTimer.h"
@implementation NSTimer (PFSafeTimer)
+ (NSTimer *)PF_ScheduledTimerWithTimeInterval:(NSTimeInterval)timeInterval block:(void(^)(void))block repeats:(BOOL)repeats {
    return [NSTimer scheduledTimerWithTimeInterval:timeInterval target:self selector:@selector(handle:) userInfo:[block copy] repeats:repeats];}
+ (void)handle:(NSTimer *)timer {
    void(^block)(void) = timer.userInfo;    if (block) {        block();    }}@end 

该方案主要要点:

  • 将计时器所应执行的任务封装成 "Block",在调用计时器函数时,把 block 作为userInfo 参数传进去。

  • userInfo 参数用来存放"不透明值",只要计时器有效,就会一直保留它。

  • 在传入参数时要通过 copy 方法,将 block 拷贝到"堆区",否则等到稍后要执行它的时候,该 blcok 可能已经无效了。

    计时器现在的 target 是 NSTimer 类对象,这是个单例,因此计时器是否会保留它,其实都无所谓。此处依然有保留环,然而因为类对象(class object)无需回收,所以不用担心。
    再调用如下:

    #import "ViewController1.h" #import "PFTimer.h" #import "NSTimer+PFSafeTimer.h"

    @interface ViewController1 ()

    //使用category @property (nonatomic, strong) NSTimer *timer1;

    @end

    @implementation ViewController1

    • (void)viewWillDisappear:(BOOL)animated {

      [super viewWillDisappear:animated]; }

    • (void)viewDidLoad { [super viewDidLoad]; self.title = @"VC1"; self.view.backgroundColor = [UIColor whiteColor];

      __weak typeof(self) weakSelf = self; self.timer1 = [NSTimer PF_ScheduledTimerWithTimeInterval:1.0 block:^{

        __strong typeof(self) strongSelf = weakSelf;
        [strongSelf timerHandle];
      

      } repeats:YES]; }

    //定时触发的事件

    • (void)timerHandle {

      NSLog(@"正在计时中。。。。。。"); }

    • (void)dealloc {

    // [self.timer stopTimer]; NSLog(@"%s",func); }

如果在 block 里面直接调用 self,还是会保留环的。因为 block 对 self 强引用,self 对 timer 强引用,timer 又通过 userInfo 参数保留 block(强引用 block),这样就构成一个环 block->self->timer->userinfo->block,所以要打破这个环的话要在 block 里面弱引用 self。

8. 使用 NSProxy 来解决循环引用

代码如下:

//PFProxy.h
#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface PFProxy : NSProxy

//通过创建对象
- (instancetype)initWithObjc:(id)object;

//通过类方法创建创建
+ (instancetype)proxyWithObjc:(id)object;

@end

NS_ASSUME_NONNULL_END

在 PFProxy.m 文件中写代码

#import "PFProxy.h"

@interface PFProxy()

@property (nonatomic, weak) id object;

@end
@implementation PFProxy

- (instancetype)initWithObjc:(id)object {

    self.object = object;
    return self;
}

+ (instancetype)proxyWithObjc:(id)object {

    return [[self alloc] initWithObjc:object];
}

- (void)forwardInvocation:(NSInvocation *)invocation {

    if ([self.object respondsToSelector:invocation.selector]) {

        [invocation invokeWithTarget:self.object];
    }
}

- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel {

    return [self.object methodSignatureForSelector:sel];
}
@end

在使用的时候如下代码:

#import "ViewController1.h"
#import "PFProxy.h"

@interface ViewController1 ()

//使用NSProxy
@property (nonatomic, strong) NSTimer *timer2;

@end

@implementation ViewController1

- (void)viewWillDisappear:(BOOL)animated {

    [super viewWillDisappear:animated];
}

- (void)viewDidLoad {

    [super viewDidLoad];
    self.title = @"VC1";
    self.view.backgroundColor = [UIColor whiteColor];

    PFProxy *proxy = [[PFProxy alloc] initWithObjc:self];
    self.timer2 = [NSTimer scheduledTimerWithTimeInterval:1.0 target:proxy selector:@selector(timerHandle) userInfo:nil repeats:YES];
}

//定时触发的事件
- (void)timerHandle {

     NSLog(@"正在计时中。。。。。。");
}

- (void)dealloc {

    [self.timer2 invalidate];
    self.timer2 = nil;
    NSLog(@"%s",__func__);
}

@end

当 pop 当前 viewController 时候,打印结果:

-[ViewController1 dealloc]

通过 PFProxy 这个伪基类(相当于 ViewController1 的复制类),避免直接让 timer 和 viewController 造成循环。