HFTimer 0.1.1

HFTimer 0.1.1

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布最后发布2016 年 7 月

helfy 维护。



HFTimer 0.1.1

  • helfy

自动释放的 Timer

想到做这个 Timer 是因为上周在修改 bug 时。发现很多同事在使用 Timer 时,没有去销毁 Timer 的意识,导致 vc 和 Timer 相互引用。所以当时就萌生了写一个会自动销毁 Timer 的想法。。然后周末下午无事。就写了一个试试。。就是现在的 HFTimer,里面的可能会有很多的坑。毕竟这是一时头脑发热做出来的。而且用的时间也不多,SO...欢迎提 bug

pod 'HFTimer', '~> 0.1.0'

自己使用 runloop 实现的一个 Timer。

相对于 NSTimer,HFTimer 有以下改变:不会强引用 target,当持有 Timer 的对象释放时,Timer 会自动销毁;支持 block,不支持 repeats,Timer 的多数需求都是需要重复,为什么不用 [target performSelector: withObject: afterDelay:]?

使用 HFTimer 需要注意的一点:必须要有对象持有它。否则将不会工作,当时做这个的初衷便是 target 和 Timer 的相互持有造成的问题,如果不持有,为什么不用 NSTimer?

因为采用的是 runLoop,所以不需要使用 RunLoopCommonModes 的,直接传入 HFRunLoopMode 即可,默认:kCFRunLoopDefaultMode

使用:

 self.timer = [HFTimer timerWithTimeInterval:1 target:self selector:@selector()];

或者

self.timer = [HFTimer timerWithTimeInterval:1 block:^(HFTimer * _Nonnull timer) {

}];