iOS如何安全而又优雅的使用NSTimer

如何安全而又优雅的使用一个NSTimer❓ 请往下看?

使用NSTimer最大的困扰就是在于必须手动释放掉这个被我们创建的timer,然而由于我们的粗心,很大可能忘记了去手动释放

 [self.timer invalidate];
 self.timer = nil;
复制代码

然而有时候我们却忘记了该在什么时机去释放呢❓

  • 我们很聪明的在dealloc中写下代码,自以为完美无缺的代码
- (void)dealloc {
    [self.timer invalidate];
    self.timer = nil;
}
复制代码

too young too simple,在dealloc中添加

NSLog(@"======= ViewController dealloc =======");

神奇的发现,居然没有打印,wtf ? ?

于是乎,各种面向搜索引擎,找到了本篇文章~ 如何安全而又优雅的使用NSTimer

先来分析下引起循环印用的原因:

假设我们在viewController中写下这样的代码时

[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(increaseLabel) userInfo:nil repeats:YES];

此时 timer->self->timerselftimer释放掉才能释放, 而timer需要等self才能释放,如此一来,造成循环引用,相互等待释放,最后,谁也没有释放掉~

思考中~?,bingo? 既然dealloc中不能写,那我在viewDidDisappear:中写可以不,试试~

测试中~...

?看到控制台输出一段字母ViewController dealloc,顿时心情很是愉悦? 这样的话,这个循环引用的问题不就解决了么~,好吧,就这样了?

too young too simple~ 这样释放掉timer会存在一个潜在的问题,假如当前这个页面的timer是在做一个模块停留计时的动作

假设是这样子的:

用户从A进入B模块,此时B开始计时, 用户从B的首页BHomeViewController进入到BSubViewController时,timer就已经停掉了 再从B返回到A时,此时的计时时间仅仅只有停留在BHomeViewController的时间

这样的结果,肯定不是我们想要的,如果在此基础上,想要得到一个较准确的计时时间,我们就必须加上一些(多余的)标记代码,来记录需要的一些状态,想一想,axb,真够low的?

于是乎,各种面向搜索引擎,还是找到了本篇文章~ 如何安全而又优雅的使用NSTimer 此处直接贴代码,代码不复杂,相信各位看官都看的懂

NSTimer+Secure.h

#import <Foundation/Foundation.h>

@interface NSTimer (Secure)
/// 默认 repeats
+ (instancetype _Nullable)seScheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id _Nonnull)aTarget selector:(SEL _Nonnull)aSelector;
+ (instancetype _Nullable)seScheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id _Nonnull)aTarget selector:(SEL _Nonnull)aSelector repeats:(BOOL)yesOrNo;

/// 默认 repeats
+ (instancetype _Nullable)seScheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id _Nonnull)aTarget block:(void (^_Nonnull)(void))block;
+ (instancetype _Nullable)seScheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id _Nonnull)aTarget repeats:(BOOL)yesOrNo block:(void (^_Nonnull)(void))block;
@end
复制代码

NSTimer+Secure.m

#import "NSTimer+Secure.h"

@interface SeTimerTarget: NSObject
@property (nonatomic, weak) id target;
@property (nonatomic, assign) SEL selector;
@property (nonatomic, weak) NSTimer *timer;

@property (nonatomic, copy) void(^block)(void);
@end

@implementation SeTimerTarget
- (void)seTimerTargetAction:(NSTimer *)timer {
    if (self.target) {
        IMP imp = [self.target methodForSelector:self.selector];
        void (*func)(id, SEL, NSTimer*) = (void *)imp;
        func(self.target, self.selector, timer);
    }
    else {
        [self.timer invalidate];
        self.timer = nil;
    }
}

- (void)seTimerBlockAction:(NSTimer *)timer {
    if (self.target && self.block) {
        self.block();
    }
    else {
        [self.timer invalidate];
        self.timer = nil;
    }
}

- (void)dealloc {
    NSLog(@"==== timer dealloc ====");
}
@end

@implementation NSTimer (Secure)

+ (instancetype)seScheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector {
    return [self seScheduledTimerWithTimeInterval:ti target:aTarget selector:aSelector repeats:YES];
}

+ (instancetype)seScheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector repeats:(BOOL)yesOrNo {
    SeTimerTarget *timerTarget = [[SeTimerTarget alloc] init];
    NSTimer *timer = [NSTimer timerWithTimeInterval:ti target:timerTarget selector:@selector(seTimerTargetAction:) userInfo:nil repeats:yesOrNo];
    timerTarget.target = aTarget;
    timerTarget.selector = aSelector;
    timerTarget.timer = timer;
    
    [[NSRunLoop mainRunLoop] addTimer:timerTarget.timer forMode:NSRunLoopCommonModes];
    return timerTarget.timer;
}

+ (instancetype)seScheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget block:(void (^)(void))block {
    return [self seScheduledTimerWithTimeInterval:ti target:aTarget repeats:YES block:block];
}

+ (instancetype)seScheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget repeats:(BOOL)yesOrNo block:(void (^)(void))block {
    SeTimerTarget *timerTarget = [[SeTimerTarget alloc] init];
    timerTarget.block = block;
    timerTarget.target = aTarget;
    NSTimer *timer = [NSTimer timerWithTimeInterval:ti target:timerTarget selector:@selector(seTimerBlockAction:) userInfo:nil repeats:yesOrNo];
    timerTarget.timer = timer;
    
    [[NSRunLoop mainRunLoop] addTimer:timerTarget.timer forMode:NSRunLoopCommonModes];
    return timerTarget.timer;
}
@end
复制代码

也可以在GitHub上去下载我的测试demo 从上面NSTimer+Secure.h中可以看出,作者提供了两种NSTimer事件回调方式,target-actionblock

在使用中,也是很方便

 [NSTimer seScheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timerAction) repeats:YES];
复制代码

或者

 __weak typeof(self) weakSelf = self;
[NSTimer seScheduledTimerWithTimeInterval:0.1 target:self block:^{
    [weakSelf timerAction];
}];
复制代码

这样使用NSTimer,就完美解决以上所说的问题,也无需关心timerviewController谁先释放的问题 安全而又优雅的code?

demo地址 以上是本篇分享的所有内容,希望对你有所帮助✌️~

转载于:https://juejin.im/post/5c8c7e195188257e3941cb82

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值