- 很多时候,我们都需要用到定时器,但是我们使用定时器的时候,稍不注意,就会出现内存泄露的问题,造成的对象无法销毁的问题
- 我们先看一下代码,我们一般是这么来运用定时器的:
- (void)viewDidLoad {
[super viewDidLoad];
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(update) userInfo:nil repeats:YES];
}
- (void)update
{
static NSInteger num = 0;
num ++;
NSLog(@"%zd",num);
}
- 的确,定时器使用起来非常简单,就是随便的像我上面一样的,写两行代码就搞定,但是里面隐藏的着一个巨大的问题,我们看一下,这个控制器,有没有得到释放。
首先:做一些提前准备,准备三个控制器,让他跳转
然后把开启定时器的控制器,设置为ViewController
我们在viewControllder中加一行代码,看他回滚到主页面的时候,释放销毁
- (void)dealloc
{
NSLog(@"我去了");
}
- 没销毁。这就是我们现在来讨论的问题。如何解决呢:我们来创建一个对象,让NSTimer对这个对象进行强引用,而不对我们的控制器进行强引用,就能解决:
.h
@interface ZYWeakTimerObject : NSObject
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(nullable id)userInfo repeats:(BOOL)yesOrNo;
@end
.m
@interface ZYWeakTimerObject()
@property(nonatomic,weak)id target;
@property(nonatomic,assign)SEL selector;
@end
@implementation ZYWeakTimerObject
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(nullable id)userInfo repeats:(BOOL)yesOrNo
{
ZYWeakTimerObject *weakObject = [[ZYWeakTimerObject alloc]init];
weakObject.target = aTarget;
weakObject.selector = aSelector;
return [NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:weakObject selector:@selector(fire:) userInfo:userInfo repeats:yesOrNo];
}
+ (void)fire:(id)obj
{
[self.target performSelector:self.selector withObject:obj];
}
+ (void)dealloc
{
NSLog(@"是否已经离去");
}
- 然后我们在ViewControllder中
@interface ViewController ()
@property(nonatomic,strong)NSTimer *timer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.timer = [ZYWeakTimerObject scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(update:) userInfo:nil repeats:YES];
}
- (void)update:(NSTimer *)timer
{
static NSInteger num = 0;
num ++;
NSLog(@"%zd",num);
}
- (void)dealloc
{
[self.timer invalidate];
NSLog(@"我去了");
}
我们再看一下,结果:
搞定