使用线程安全的 MSWeakTimer ,它不会对目标进行retain操作,避免循环引用

简介:

MSWeakTimer

简易翻译:该timer没有runloop概念,线程安全,没有循环引用现象。

https://github.com/mindsnacks/MSWeakTimer

Description

Thread-safe NSTimer drop-in alternative that doesn't retain the target and supports being used with GCDqueues.

这是个线程安全的 NSTimer,他不会对目标进行retain操作,而且,他还支持GCDqueues。

Motivation

The first motivation for this class was to have a type of timer that objects could own and retain, without this creating a retain cycle ( like NSTimer causes, since it retains its target ). This way you can just release the timer in the -dealloc method of the object class that owns the timer.

写这个类的动机是,我想使用这样的NSTimer,他不会对object进行retain操作,而且,更不会出现循环引用(系统的NSTimer就会出现这个问题)。然后呢,你可以在-dealloc方法中来释放这个timer。

The other problem when using NSTimer is this note on the documentation:

另一个使用NSTimer会出现的问题在以下文档中提出了:

Special Considerations

You must send this message from the thread on which the timer was installed. If you send this message from another thread, the input source associated with the timer may not be removed from its run loop, which could prevent the thread from exiting properly.

需要特别注意的地方

你必须在当前的线程中给你创建的NSTimer发送信息,如果你是从另外一个线程中发过来的信息,那么,这个timer可能就不会从他当前的run loop中移除,那个会很出现问题的。

More often than not, an object needs to create a timer and invalidate it when a certain event occurs. However, doing this when that object works with a private GCD queue gets tricky. This timer object is thread safe and doesn't have the notion of run loop, so it can be used with GCD queues and installed / invalidated from any thread or queue.

不管怎样,一个对象创建出一个定时器,当触发了几次事件后,就销毁这个定时器。然而,你如果在GCD队列中来销毁这个定时器,效果非常微妙(有可能出现内存泄露)。而大哥我写的这个定时器是线程安全的,他不存在run loop的概念,所以,他可以在任何的GCDqueue中进行初始化以及移除掉。

Related Stackoverflow question.

Implementation

The implementation of MSWeakTimer was reviewed and validated by a libdispatch (GCD) engineer at the WWDC 2013 Core OS Lab.

How to Use

Create an MSWeakTimer object with this class method:

+ (MSWeakTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)timeInterval
                                         target:(id)target
                                       selector:(SEL)selector
                                       userInfo:(id)userInfo
                                        repeats:(BOOL)repeats
                                  dispatchQueue:(dispatch_queue_t)dispatchQueue;
目录
相关文章
|
3月前
|
C++
记录一次循环引用的问题
记录一次循环引用的问题
|
1月前
|
存储 安全 Java
调用链跨线程传递 ThreadLocal 对象对比
说起本地线程专属变量,大家首先会想到的是 JDK 默认提供的 ThreadLocal,用来存储在整个链路中都需要访问的数据,并且是线程安全的。由于在落地全链路压测的过程中,一个基本并核心的功能需求是流量标记需要在整个链路中进行传递,那么线程上下文环境成为解决这个问题最合适的技术。
40 2
调用链跨线程传递 ThreadLocal 对象对比
|
2月前
|
安全 编译器 C++
C++类与对象【对象的初始化和清理】
C++类与对象【对象的初始化和清理】
C++类与对象【对象的初始化和清理】
|
2月前
|
存储 安全
除了Lock对象,还有其他方法可以实现多线程安全的单例模式吗?
【2月更文挑战第5天】【2月更文挑战第12篇】除了Lock对象,还有其他方法可以实现多线程安全的单例模式吗?
|
9月前
|
存储 数据库
如何解决循环引用的问题
解决循环引用的问题
106 0
|
缓存 Java
ThreadLocal引用分析
ThreadLocal引用分析
|
C# 图形学
U3D客户端框架之基于类对象池的可回收变量代码实现
可回收变量 是基于引用计数去做的管理,基类实现了引用计数增加,和释放功能,当调用引用计数增加函数时,引用计数+1;当调用释放函数时候,引用计数-1,当引用计数==0的时候,类对象池会把该可回收变量对象回收。
|
缓存 Java 关系型数据库
强引用、软引用、弱引用、幻象引用有什么区别和使用场景
强引用、软引用、弱引用、幻象引用有什么区别和使用场景
155 1
|
iOS开发
iOS开发:block死循环及__weak弱引用提前释放的问题解决
block死循环及__weak弱引用提前释放的问题解决
289 0
|
存储 IDE 数据可视化
hashCode竟然不是根据对象内存地址生成的?还对内存泄漏与偏向锁有影响?(下)
hashCode竟然不是根据对象内存地址生成的?还对内存泄漏与偏向锁有影响?(下)
hashCode竟然不是根据对象内存地址生成的?还对内存泄漏与偏向锁有影响?(下)