iOS | 用于处理循环引用的block timer
来源:无夜之星辰     阅读:755
蜂鸟
发布于 2018-12-12 23:43
查看主页

iOS 10的时候NSTimer新添加了一个带block的API:

+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)interval repeats:(BOOL)repeats block:(void (^)(NSTimer *timer))block API_AVAILABLE(macosx(10.12), ios(10.0), watchos(3.0), tvos(10.0));

苹果的官方文档里说,将这个timer本身作为参数传给block以此来避免循环引用:

/// - parameter: block The execution body of the timer; the timer itself is passed as the parameter to this block when executed to aid in avoiding cyclical references

有了这个API再也不需要繁琐的手动注销timer,结合weakSelf即可以轻松解决循环引用,如:

__weak typeof(self) weakSelf = self;self.timer = [NSTimer scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {    __strong typeof(self) strongSelf = weakSelf;    [strongSelf printNum];}];

在这个API出现之前,self和timer的引用关系是:
self->timer->self

现在的引用关系是:
self->timer->weakSelf

但是只有iOS 10及之后的系统才能使用此API,而我们一般都是适配到iOS 8,所以有必要扩展一下。

如何扩展?

简单点,写个category,直接复制苹果的API进去(思考API设计的时间都省了??),而后加上前缀:

+ (NSTimer *)cq_scheduledTimerWithTimeInterval:(NSTimeInterval)interval repeats:(BOOL)repeats block:(void (^)(NSTimer *timer))block {    return [self scheduledTimerWithTimeInterval:interval target:self selector:@selector(cq_callBlock:) userInfo:[block copy] repeats:repeats];}+ (void)cq_callBlock:(NSTimer *)timer {    void (^block)(NSTimer *timer) = timer.userInfo;    !block ?: block(timer);}

你不是把timer作为参数传给block吗?那我也这样搞。

而后即可以像使用系统API那样使用了:

__weak typeof(self) weakSelf = self;self.timer = [NSTimer cq_scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer *timer) {    __strong typeof(self) strongSelf = weakSelf;    [strongSelf printNum];}];

最后提供一个此timer使用的具体demo:
CaiWanFeng/CQCountDownButton

已是最前 目录 下一篇
免责声明:本文为用户发表,不代表网站立场,仅供参考,不构成引导等用途。 系统环境 服务器应用
相关推荐
Java 异常解决
Swift 优雅的适配大小
HDFS_DELEGATION_TOKEN 复原及处理方案
记录一次数据库死锁问题处理方案
Linux Kernel 4.18-rc7 发布,Linux 4.18 或者于下周推出
首页
搜索
订单
购物车
我的