在使用NSTimer,若是使用不得当特别会引发循环引用,形成内存泄露。因此怎么避免循环引用问题,下面我提出几种解决NSTimer的几种循环引用。ios
当你在ViewController(简称VC)中使用timer属性,因为VC强引用timer,timer的target又是VC形成循环引用。当你在VC的dealloc方法中销毁timer,
发现VC被pop,VC的dealloc方法没走,VC在等timer释放才走dealloc,timer释放在dealloc中,因此引发循环引用。macos
代码以下:bash
//PFTimer.h文件
#import <Foundation/Foundation.h>
@interface PFTimer : NSObject
//开启定时器
- (void)startTimer;
//暂停定时器
- (void)stopTimer;
@end
复制代码
在PFTimer.m文件中代码以下:app
#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__);
}
复制代码
运行打印结果:ui
-[ViewController1 dealloc]
-[PFTimer dealloc]
复制代码
这个方式主要就是让PFTimer强引用NSTimer,NSTimer强引用PFTimer,避免让NSTimer强引用ViewController,这样就不会引发循环引用,而后在dealloc方法中执行NSTimer的销毁,相对的PFTimer也会进行销毁了。this
在iOS 10.0之后,苹果官方新增了关于NSTimer的三个API:atom
+ (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参数,官方文档有说明:spa
the timer itself is passed as the parameter to this block when executed
to aid in avoiding cyclical references。
复制代码
翻译过来就是说,定时器在执行时,将自身做为参数传递给block,来帮助避免循环引用。使用很简单,可是要注意两点:翻译
1.避免block的循环引用,使用__weak和__strong来避免
2.在持用NSTimer对象的类的方法中-(void)dealloc调用NSTimer 的- (void)invalidate方法;
经过建立一个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。
原理以下图:
代码以下:
//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形成循环。
原文地址:https://www.jianshu.com/p/fca3bdfca42f