首先有两个概念写在最前:api
内存泄漏:系统分配的内存空间在使用完毕以后没有进行及时的回收,称之为发生了内存泄漏。bash
内存溢出:指在申请内存的时候,没有足够的内存空间可使用,包括栈溢出和堆溢出。函数
下面开始啦: 首先,建立出一个循环引用, 建立一个TestViewController,建立一个timer,ui
_timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(fire) userInfo:nil repeats:YES];
-(void)fire{
NSLog(@"fire");
}
-(void)dealloc
{
NSLog(@"%@ delloc",self);
}
复制代码
运行上述代码,而后TestViewController pop回去以后,你会发现fire一直在打印,此时就会形成一个循环引用的问题,本质的缘由实际上是NSTimer对当前的target(self)是一个强引用,在强引用的过程当中他们相互持有,因此他们之间就没有办法正常释放。可能有同窗会说在析构函数dealloc中将 [_timer invalidate]; _timer = nil; 其实运行下来,你会发现dealloc函数没有执行。咱们都知道,dealloc是每一个控制器中都有的一个系统方法,由系统响应执行,当前控制器销毁时,dealloc就会被执行,但循环引用形成的当前类没有被销毁。atom
接下来,咱们就去解决这个NSTimer的循环引用问题:spa
1.第一种方法:在合适的时机销毁NSTimercode
-(void)didMoveToParentViewController:(UIViewController *)parent
{
//parent == nil 当父视图为空(iOS8.0以后提供的api,用来管理子视图的生命周期)
if (!parent) {
[_timer invalidate];
_timer = nil;
}
}
复制代码
2.第二种方法:引入中间者对象
其实就是把当前的强引用转到target,若是当前的viewcontroller可以正常回收,那么dealloc方法就可以正常执行。生命周期
@property (nonatomic,strong) id target;
//建立一个target对象,
_target = [NSObject new];
对于当前这个_target来讲,本质上是要做为消息的处理者,显然_target须要一个selector,因此咱们动态添加一个方法到当前对象上来,
class_addMethod([_target class], @selector(fire), class_getMethodImplementation([self class], @selector(fire)), "v@:");
_timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:_target selector:@selector(fire) userInfo:nil repeats:YES];
-(void)dealloc
{
NSLog(@"%@ delloc",self);
[_timer invalidate];
_timer = nil;
}
此时viewcontroller的析构函数就能够正常执行。
复制代码
3.第三种方法:高级的中间者内存
此时咱们须要借助一个虚基类NSProxy,(NSProxy其主要用来消息转发的处理)
// HZProxy.h
#import <Foundation/Foundation.h>
@interface HZProxy : NSProxy
//仍是要有个target
@property (nonatomic,weak) id target;
@end
// HZProxy.m
#import "HZProxy.h"
@implementation HZProxy
//获取当前的方法签名
-(NSMethodSignature *)methodSignatureForSelector:(SEL)sel
{
return [self.target methodSignatureForSelector:sel];
}
//指定当前消息的处理者
-(void)forwardInvocation:(NSInvocation *)invocation
{
[invocation invokeWithTarget:self.target];
}
//执行timer
//虚基类只有alloc方法,因此初始化,直接调用alloc
_hzProxy = [HZProxy alloc];
//当前Proxy的target设为当前的self,由于真正要处理消息的实际上是当前的viewcontroller(其实这个target就至关于delegate)
_hzProxy.target = self;
_timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:_hzProxy selector:@selector(fire) userInfo:nil repeats:YES];
当前_timer的对象的处理就变成了_hzProxy
运行一下,能够看到viewcontroller的析构函数能够正常执行
复制代码
4.第四种:带block的timer
在咱们建立timer的时候,苹果也意识到NSTimer的api是存在必定问题的,因此在iOS10.0以后提供了一种block的方法来去解决NSTimer的循环引用的问题。
__weak typeof(self) weakSelf = self;
_timer = [NSTimer scheduledTimerWithTimeInterval:1.0f repeats:YES block:^(NSTimer * _Nonnull timer) {
__strong typeof(self) strongSelf = weakSelf;
[strongSelf fire];
}];
复制代码
可是为了兼容当前的api在iOS10.0以前的状况,因此这个时候咱们能够HOOK一下
首先咱们建立一个NSTimer的分类,
// NSTimer+ZHTimer.h
#import <Foundation/Foundation.h>
@interface NSTimer (ZHTimer)
+(NSTimer *)zh_scheduledTimerWithTimeInterval:(NSTimeInterval)interval repeats:(BOOL)repeats block:(void (^)(void))block;
@end
// NSTimer+ZHTimer.m
#import "NSTimer+ZHTimer.h"
@implementation NSTimer (ZHTimer)
+(NSTimer *)zh_scheduledTimerWithTimeInterval:(NSTimeInterval)interval repeats:(BOOL)repeats block:(void (^)(void))block
{
return [self scheduledTimerWithTimeInterval:interval target:self selector:@selector(zh_blockHandle:) userInfo:block repeats:YES];
//这里面这个self,其实指的是当前的类对象,在内存中只存在一份,就是以单例的形式存在,因此咱们在每一次建立实例变量都要经过这个类对象来建立,
//因此并不须要担忧当前的target形成循环引用,由于单例不须要被释放,只有当APP被Q的时候,存在内存中的单例才会被释放掉。
}
+(void)zh_blockHandle:(NSTimer *)timer{
void(^block)(void) = timer.userInfo;
if (block) {
block();
}
}
@end
//调用一下
__weak typeof(self) weakSelf = self;
_timer = [NSTimer zh_scheduledTimerWithTimeInterval:1.0f repeats:YES block:^{
__strong typeof(self) strongSelf = weakSelf;
[strongSelf fire];
}];
复制代码
以上就是我的对NSTimer循环引用的理解以及处理方法,可能不是很完善,很深刻,有Bug的地方还请指出,很是感谢。