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;

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

  1. WCF 遇到 由于线程退出或应用程序请求,已放弃 I/O 操作 ListenerContextInputStream

    异常类型:IOException 异常消息:An exception has been thrown when reading the stream. 异常信息: at System.ServiceM ...

  2. QThread::wait(),一直以来我以为它阻塞的是QThread对象,可是我现在明白,原来阻塞的是这个对象所在的线程(通常是主线程)——所有事情源于 QThread 的事件循环——如果使用继承QThread这一方法,QThread::quit()没有效果,因为这个线程根本就不需要事件循环

    近日,使用QThread,一些问题百思不得其解,看过大牛的文章,恍然大悟啊. 原文 http://hi.baidu.com/dbzhang800/item/c14c97dd15318d17e1f46f ...

  3. 给线程发送消息让它执行不同的处理(自己建立消息循环,非常有意思) good

    unit Unit2; interface usesSystem.Classes, Windows, Messages; constWM_DO = WM_USER + 1; typeTDemoThre ...

  4. 三个线程按循序一个打印A一个打印B一个打印C 循环打印?

    第一种 public static volatile int flag = 1; public static void printABC1(){ Thread t1 = new Thread(() - ...

  5. IOS学习之路--OC的基础知识

    1.项目经验 2.基础问题 3.指南认识 4.解决思路 ios开发三大块: 1.Oc基础 2.CocoaTouch框架 3.Xcode使用 -------------------- CocoaTouc ...

  6. iOS求职之OC面试题

    1.Objective-C的类可以多重继承么?可以采用多个协议么? 答:不可以多重继承,可以采用多个协议. 2.#import和#include的区别是什么?#import<> 跟 #im ...

  7. object-c 的ARC 问答/介绍

    原文:http://blog.csdn.net/kmyhy/article/details/8895606 概念" Clangstatic analyzer "是一个非常有用的查找 ...

  8. ios问答题与答案

    1. OC中,与alloc语义相反的方法是dealloc还是release?与retain语义相反的方法是dealloc还是release?为什么?需要与alloc配对使用的方法是dealloc还是r ...

  9. 深入理解 GCD

    前言 首先提出一些问题: dispatch_async 函数如何实现,分发到主队列和全局队列有什么区别,一定会新建线程执行任务么? dispatch_sync 函数如何实现,为什么说 GCD 死锁是队 ...

随机推荐

  1. 铁轨(UVa 514)

    利用栈实现 C++11 代码如下: #include<iostream> #include<stack> using namespace std; #define maxn 1 ...

  2. day6 shelve模块

        shelve模块 shelve模块是一个简单的k,v将内存数据通过文件持久化的模块,可以持久化任何pickle可支持的python数据格式,shelve模块是对pickle模块的补充.我们知道 ...

  3. 【UOJ】#37. 【清华集训2014】主旋律

    题解 一道,神奇的题= = 我们考虑正难则反,我们求去掉这些边后有多少图不是强连通的 怎么求呢,不是强连通的图缩点后一定是一个DAG,并且这个DAG里面有两个点 我们想一下,如果我们把1当成入度为0的 ...

  4. Ionic入门十:icon(图标)

    ionic 也默认提供了许多的图标,大概有500多个.用法也非常的简单: <i class="icon ion-star"></i> 图标列表如下:   ...

  5. poi类包对比

  6. 三、django rest_framework源码之权限流程剖析

    1 绪言 上一篇中分析了认证部分的源码,认证后的下一个环节就是权限判定了.事实上,权限判定肯定要与认证联合使用才行,因为认证部分不会对请求进行禁止或者是允许,而只是根据请求中用户信息进行用户身份判断, ...

  7. 机器学习:KNN-近邻算法

    一.理论知识 1.K近邻(k-Nearest Neighbor,简称KNN)学习是一种常用的监督学习. 工作机制:给定测试样本,基于某种距离度量找出训练集中与其最靠近的k个训练样本,然后基于这k个的信 ...

  8. <泛> 归并排序 及 逆序对

    今天写一个归并排序的模板,返回值为该序列的逆序对数 基本思路 归并排序就是利用二分的思想,将区间无限递归二分,直到当前划分区间只包含一个元素或没有元素的时候(我们认为这个序列是自动有序的),我们回溯到 ...

  9. Cpp下匿名对象探究

    先来看看一段代码: #include "iostream" using namespace std; class ABCD { public: ABCD(int a, int b, ...

  10. php常见网络攻击及防御方法

    常见的Web攻击分为两类:一是利用Web服务器的漏洞进行攻击,如CGI缓冲区溢出,目录遍历漏洞利用等攻击;二是利用网页自身的安全漏洞进行攻击,如SQL注入,跨站脚本攻击等.下面这篇文章主要介绍了PHP ...