使用线程安全的 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 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操作,避免循环引用的更多相关文章
- WCF 遇到 由于线程退出或应用程序请求,已放弃 I/O 操作 ListenerContextInputStream
异常类型:IOException 异常消息:An exception has been thrown when reading the stream. 异常信息: at System.ServiceM ...
- QThread::wait(),一直以来我以为它阻塞的是QThread对象,可是我现在明白,原来阻塞的是这个对象所在的线程(通常是主线程)——所有事情源于 QThread 的事件循环——如果使用继承QThread这一方法,QThread::quit()没有效果,因为这个线程根本就不需要事件循环
近日,使用QThread,一些问题百思不得其解,看过大牛的文章,恍然大悟啊. 原文 http://hi.baidu.com/dbzhang800/item/c14c97dd15318d17e1f46f ...
- 给线程发送消息让它执行不同的处理(自己建立消息循环,非常有意思) good
unit Unit2; interface usesSystem.Classes, Windows, Messages; constWM_DO = WM_USER + 1; typeTDemoThre ...
- 三个线程按循序一个打印A一个打印B一个打印C 循环打印?
第一种 public static volatile int flag = 1; public static void printABC1(){ Thread t1 = new Thread(() - ...
- IOS学习之路--OC的基础知识
1.项目经验 2.基础问题 3.指南认识 4.解决思路 ios开发三大块: 1.Oc基础 2.CocoaTouch框架 3.Xcode使用 -------------------- CocoaTouc ...
- iOS求职之OC面试题
1.Objective-C的类可以多重继承么?可以采用多个协议么? 答:不可以多重继承,可以采用多个协议. 2.#import和#include的区别是什么?#import<> 跟 #im ...
- object-c 的ARC 问答/介绍
原文:http://blog.csdn.net/kmyhy/article/details/8895606 概念" Clangstatic analyzer "是一个非常有用的查找 ...
- ios问答题与答案
1. OC中,与alloc语义相反的方法是dealloc还是release?与retain语义相反的方法是dealloc还是release?为什么?需要与alloc配对使用的方法是dealloc还是r ...
- 深入理解 GCD
前言 首先提出一些问题: dispatch_async 函数如何实现,分发到主队列和全局队列有什么区别,一定会新建线程执行任务么? dispatch_sync 函数如何实现,为什么说 GCD 死锁是队 ...
随机推荐
- Alpine里的用户管理命令
注意噢,和普通的linux不一样的. 1, 建立一个指定GID的组: addgroup -g 10001 -S groupA 2, 建立一个指定UID的用户,指定shell, 让它属于指定的用户组. ...
- HP 打印机监控
http://www.ttlsa.com/zabbix/zabbix-monitor-hp-printer/ https://www.cnblogs.com/losbyday/articles/583 ...
- 面试题46:求1+2+ …… +n
题目:求1+2+…+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字以及条件判断语句(A?B:C). 通常求1+2+…+n除了用公式n(n+1)/2之外,无外 ...
- bzoj 1899 贪心+dp
思路:这个贪心排顺序我居然没看出来. 吃饭时间长的在前面, 用反证法很容易得出. 剩下的就是瞎dp啦. #include<bits/stdc++.h> #define LL long lo ...
- SQLite数据库、ListView控件的使用
android下数据库的创建(重点) 在Android平台上,集成了一个轻量级嵌入式关系型数据库—SQLite,SQLite3支持 NULL.INTEGER.REAL(浮点数字).TEXT(字符串文本 ...
- Request常用方法(转)
原文地址:http://www.lihuai.net/program/python/1617.html Python Requests库:HTTP for Humans 时间: 2014/12/30 ...
- 基于Laravel开发博客应用系列 —— 使用Bower+Gulp集成前端资源
本节我们将讨论如何将前端资源集成到项目中,包括前端资源的发布和引入.本项目将使用 Bower 和 Gulp 下载和集成jQuery.Bootstrap.Font Awesome 以及 DataTabl ...
- CodeForces - 620C Pearls in a Row 贪心 STL
C. Pearls in a Row time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- JAVA 获取分行符
public static final String CR_LF = System.getProperty("os.name").startsWith("Windows& ...
- 【SQL】183. Customers Who Never Order
Suppose that a website contains two tables, the Customers table and the Orders table. Write a SQL qu ...