NSTimer是我们平时在项目中使用比较多的,但是使用的时候需要比较注意,需要在目标对象释放之前就要结束定时器,不然会造成内存泄露。
具体原因就是NSTimer的常用的方法:
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(nullable id)userInfo repeats:(BOOL)yesOrNo;
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(nullable id)userInfo repeats:(BOOL)yesOrNo;
这两个方法需要设置target,但是系统内部会对target强引用,导致target无法释放。
有的时候我们希望定时器在目标对象释放的时候能够自己结束,但是我们又无法直接修改系统内部的api。
这个时候我们可以利用利用一种比较简单地思路来实现:引入一个中间对象TimerDelegate,
将定时器的目标对象设为TimerDelegate,再由TimerDelegate去调用原target的方法,这样每次调用之前我们都可以知道target是否已经被释放了。
实现:
新建对象TimerDelegate
// TimerDelegate.h
#import <Foundation/Foundation.h>
@interface TimerDelegate : NSObject
- (void)handleTarget:(id)aTarget selector:(SEL)aSel userInfo:(id)userInfo;
@property (weak,nonatomic)id desTarget; //注意弱引用
@property (assign,nonatomic)SEL selector;
@property (strong,nonatomic)id userInfo;
用runtime交换这两个方法
// NSTimer+Release.m
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSArray *selNameArray = @[@"scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:",@"timerWithTimeInterval:target:selector:userInfo:repeats:"];
for (NSString *name in selNameArray) {
SEL oriSel = NSSelectorFromString(name);
NSString *newSelString = [NSString stringWithFormat:@"YT_%@",NSStringFromSelector(oriSel)];
SEL newSel = NSSelectorFromString(newSelString);
Class class = object_getClass((id)self);
[RuntimeManager swizzleClass:class OrigionMethod:oriSel withNewMethod:newSel];
}
});
}
如果定时器不重复则不需要处理
+ (NSTimer *)YT_scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(nullable id)userInfo repeats:(BOOL)yesOrNo{
if(yesOrNo) {
TimerDelegate *timerDelegate = [[TimerDelegate alloc]init];
//将原目标对象和参数传递给delegate
[timerDelegate handleTarget:aTarget selector:aSelector userInfo:userInfo];
SEL sel = NSSelectorFromString(@"fireTimer:");
//调用初始的方法,此处将定时器对象设置TimerDelegate, 然后调用TimerDelegate对象的fireTimer方法
NSTimer* timer = [NSTimer YT_scheduledTimerWithTimeInterval:ti target:timerDelegate selector:sel userInfo:userInfo repeats:yesOrNo];
return timer;
}
else {
return [self YT_scheduledTimerWithTimeInterval:ti target:aTarget selector:aSelector userInfo:userInfo repeats:yesOrNo];
}
}
然后实现NStimerDelegate.m文件里的方法:
// NSTimerDelegate.m
- (void) fireTimer:(NSTimer *)timer {
if(!_desTarget) { //判断原对象是否已经释放
[timer invalidate];
timer = nil;
return ;
}
//执行原目标对象的方法
if ([_desTarget respondsToSelector:self.selector]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[self.desTarget performSelector:self.selector withObject:timer];
#pragma clang diagnostic pop
}
}