NSTimer使用时注意项总结

本文总结了NSTimer的使用方法,强调了在设置repeats为YES时可能导致的内存泄露问题,并提供了几种解决计时器无法释放的方案,包括在特定生命周期方法中设置失效、利用消息转发机制和使用弱引用等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、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)

解决办法:

  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;
        }
    }

     

  3. 利用消息转发机制
//利用中间键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 <objc/runtime.h>
    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;
}

4.中间键弱引用self

// runloop强引用timer,timer强引用proxy, proxy弱引用self。(弱引用不会使self的引用计数加一)
runloop -> timer -> proxy --> self(UIViewController)

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

示例代码:

// 创建NSProxy类
#import <Foundation/Foundation.h>

@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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值