FoundatationiOS

iOS-NSTimer释放的三种方式

2019-05-28  本文已影响0人  厦门_小灰灰

一开始在使用NSTimer的时候,会发现出现了循环引用。

出现循环引用是因为

将一个NSTimer的对象作为一个ViewController(VC)的的属性,当前VC对NSTimer对象进行了一次强引用。在创建NSTimer的时候,NSTimer对象又将当前VC作为自己的target,这时NSTimer对象对当前VC进行了一次强引用,这样就造成了NSTimer和当前VC的循环引用,从而让VC和NSTimer都无法释放,最终导致内存泄漏。

代码如下:

self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];

其中 timer是VC的属性

@property (strong, nonatomic) NSTimer *timer;
释放定时器
- (void)cleanTimer {
    if ( _timer ) {
        [_timer invalidate];
        _timer = nil;
    }
}

我们需要在合适的时机释放NSTimer。如:viewWillDisappear,viewDidDisappear,dealloc。其中前面两种要根据具体业务,第三种dealloc并不会执行,因为dealloc的执行时机是在self释放之后执行的,这时候timer还是强引用着self,导致self无法释放,以至于无法执行dealloc。

所以我们现在要解决循环引用的问题。

第一种

使用一个中间的对象来当做timer的target

添加一个属性
@property (strong, nonatomic) id target;

然后初始化
self.target = [NSObject new];

//然后给这个对象的类添加方法和方法的实现
class_addMethod([self.target class], @selector(timerAction), (IMP)timerAct, "v@:");

方法的实现
void timerAct(id self, SEL _cmd) {
    NSLog(@"timerAct fire ....");
}

这时候初始化timer的时候,将target指向self.target
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self.target selector:@selector(timerAction) userInfo:nil repeats:YES];

然后在dealloc析构方法中释放timer
- (void)dealloc
{
    [self cleanTimer];
    NSLog(@"%@ is dealloc", [self class]);
}

结论:
使用中间对象,可以让self和timer不出现强引用,self和self.timer都指向target。所以当VC消失的时候,会走dealloc方法,然后释放timer。

第二种

利用一个含有weak属性的对象A包裹self作为target,再对A进行消息转发,访问A就相当于访问self

这里使用NSProxy的子类来操作。
.h文件

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface LHProxy : NSProxy

@property (weak, nonatomic) id target;

@end

NS_ASSUME_NONNULL_END

.m文件

#import "LHProxy.h"
#import <objc/runtime.h>

@implementation LHProxy

- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel {
    return [self.target methodSignatureForSelector:sel];
}

- (void)forwardInvocation:(NSInvocation *)invocation {
    [invocation invokeWithTarget:self.target];
}

@end

然后在VC中

添加一个属性
@property (strong, nonatomic) LHProxy *lh_proxy;

初始化,并将内部weak属性指向self
self.lh_proxy = [LHProxy alloc];
self.lh_proxy.target = self;

初始化timer,并设置target为self.lh_proxy
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self.lh_proxy selector:@selector(timerAction) userInfo:nil repeats:YES];

结论:target是内部weak属性指向self,相当于target拥有self且是weak,self的retain没有加1,timer拥有LHProxy对象target,target的retain加1,timer和self的直接关系是timer仅是self的一个属性;

第三种

注:以下这个方式是iOS10才出现的,使用block来解决NSTimer循环引用的问题,暂时不考虑下面这种方法。

+ (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));
__weak typeof(self) weakSelf = self;
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 repeats:YES block:^(NSTimer * _Nonnull timer) {
        [weakSelf timerAction];
    }];

注意block也会造成循环引用哦~~~

上一篇下一篇

猜你喜欢

热点阅读