使用线程安全的 MSWeakTimer ,它不会对目标进行retain操作,避免循环引用
来源:互联网 发布:pkpm软件pmsap什么锁 编辑:程序博客网 时间:2024/05/29 10:57
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 GCD
queues.
这是个线程安全的 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;
- 使用线程安全的 MSWeakTimer ,它不会对目标进行retain操作,避免循环引用
- matlab中对数字图像进行像素操作时,尽量避免使用多层嵌套FOR循环
- retain的循环引用注意以及@class的使用
- shared_ptr:线程安全、循环引用
- shared_ptr:线程安全、循环引用
- shared_ptr:线程安全、循环引用
- shared_ptr:线程安全、循环引用
- 避免block的循环引用
- 避免Block的循环引用
- mysql查询数据的同时对它进行删除操作
- 视频目标检测中关于对检测出的目标进行”安全处理“问题
- 使用block时如何避免循环引用
- 循环引用, 如果都是对象, 使用 retain + assign
- NSArray的addObject会对obj进行retain操作。NSArray的release或removeObjectAt会对元素进行release
- NSArray的addObject会对obj进行retain操作。NSArray的release或removeObjectAt会对元素进行release
- 对 Windows 窗体控件进行线程安全调用的方法
- 使用SQL游标对数据进行遍历循环操作
- 线程间操作无效: 从不是创建控件(对 Windows 窗体控件进行线程安全调用)
- VC 为程序创建快捷方式的详细讲解
- jquery和DOM比较
- escape,encodeURI,encodeURIComponent 解决乱码
- fgets函数
- CloudFoundry部署(一):IAAS虚拟化环境(VMWare)准备
- 使用线程安全的 MSWeakTimer ,它不会对目标进行retain操作,避免循环引用
- Flex与JSON及XML的互操作
- android4.4源码编译环境搭建
- 使用开源库 JGDownloadAcceleration 控制下载队列,断点下载,加速下载
- CentOS升级Git
- ie10 兼容性调整之滚动条消失的修正思路 scrollbar|lightbox
- Hibernate源码分析
- ctype.h
- Service与Android系统设计(6)--- Native Service