iOS NSTimer遇坑整理

一、NSTimer使用

const NSTimeInterval TimeInterval = 1.0;

@interface UIViewController ()
// 定义属性timer
@property (nonatomic, strong) NSTimer *timer;
@end

/**
 * timer 初始化
 * repeats:参数表示是否重复执行(YES表示每TimeInterval秒运行一次function方法。NO表示不重复只调用 一次,timer运行一次就会自动停止运行)
*/

self.timer =  [NSTimer scheduledTimerWithTimeInterval:TimeInterval target:self selector:@selector(fire) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];

注意:将计数器的repeats设置为YES的时候,self的引用计数会加1。因此可能会导致self(即VC)不能release,所以在UIViewController delloc前将计数器timer设置为失效,否则可能会导致内存泄露。

//开启定时器
[self.timer fire];

//暂停定时器(然后再某种情况下再次开启运行timer)
self.timer.fireDate = [NSDate distantFuture];

//再次开启定时器
self.timer.fireDate = [NSDate distantPast];

//取消定时器(这个是永久的停止)
[self.timer invalidate];

// 停止后,一定要将timer赋空,否则还是没有释放
self.timer = nil;

通常写到这会遇到诸多问题。

二、NSTimer常见问题

当repeats为YES时timer出现无法释放的问题(强引用,而非循环引用引起)

// runloop强引用timer,timer强引用self。如果timer不失效,self就不会释放,进而造成内存泄漏
runloop -> timer -> self(UIViewController)

解决办法有4种:

  1. 也是最low的方法在控制器消失的时候(viewDidDisappear方法中)设置timer失效,但是会出现一些问题,跳转下一级界面的时候timer失效,再返回的时候还得在界面出现(viewWillAppear方法中)时从新设置timer,比较繁琐,控制不好还会问题。不推荐使用

  2. 在UIViewController中调用didMoveToParentViewController:方法设置timer失效

// 这种方法只有在进入VC时使用的push的方式进入才有效,present进入不会调用此方法
- (void)didMoveToParentViewController:(UIViewController *)parent{
    if (!parent) {
        [self.timer invalidate];
        self.timer = nil;
    }
}
  1. 利用消息转发机制
//利用中间键target 不让timer来强用self
runloop -> timer -> target

示例代码:

@interface UIViewController ()
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, strong) id target;
@end

- (void)creatTimer{
    _target = [NSObject new];

    //我们需要使用runtime来给_target添加方法,引入头文件 #import 
    class_addMethod([_target class], @selector(fire), (IMP)fireIMP, "v@:");

    //timer 的target直接指向_target
    self.timer = [NSTimer scheduledTimerWithTimeInterval:TimeInterval target:_target  selector:@selector(fire) userInfo:nil repeats:YES];

    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

void fireIMP(id self, IMP _cmd) {
    NSLog(@"重复跑起来");
}

// 这样只需要在VC 的dealloc方法中设置timer失效即可
- (void)dealloc{
    [self.timer invalidate];
    self.timer = nil;
}
  1. 中间键弱引用self
// runloop强引用timer,timer强引用proxy, proxy弱引用self。(弱引用不会使self的引用计数加一)
runloop -> timer -> proxy --> self(UIViewController)

用一个比NSObjec更轻量级的类NSProxy来做中间键

示例代码:

// 创建NSProxy类
#import 

@interface WeakProxy : NSProxy
//使用弱引用
@property (nonatomic, weak) id target;
@end

#import "WeakProxy.h"

@implementation WeakProxy

/**
 * - (NSMethodSignature *)methodSignatureForSelector:(SEL)sel
 * - (void)forwardInvocation:(NSInvocation *)invocation
 * 这两个方法必须写
*/

// 方法签名
- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel{
      return [self.target methodSignatureForSelector:sel];
}

// 消息转发
- (void)forwardInvocation:(NSInvocation *)invocation{
    [invocation invokeWithTarget:self.target];
}
@end

回到UIViewController中

- (void)creatTimer{
   // 因为在上述类中没有写构造函数直接alloc。
    WeakProxy *proxy =  [WeakProxy alloc];
    // 弱引用self
    proxy.target = self;

    //timer 的target直接指向proxy
    self.timer =  [NSTimer scheduledTimerWithTimeInterval:TimeInterval target:proxy  selector:@selector(fire) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

// 然后只需在delloc中实现计时器的销毁即可
- (void)dealloc{
    [self.timer invalidate];
    self.timer = nil;
}

如果有什么问题请提出,共同讨论解决。

你可能感兴趣的:(iOS NSTimer遇坑整理)