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. SSIS 学习之旅 序章 和 简介

    SSIS 学习之旅目录: 第一章: SSIS 学习之旅 第一个SSIS 示例(一) 第二章: SSIS 学习之旅 第一个SSIS 示例(二) 第三章: SSIS 学习之旅 数据同步 第四章: SSIS ...

  2. git clone 某个分支或者所有分支

    clone 某个分支: git clone -b  dev5   https://git.coding.net/aiyongbao/tradepc.git clone 所有分支:   git   cl ...

  3. bzoj 2756

    2756 思路: 二分讨论,网络流检验: 代码: #include <bits/stdc++.h> using namespace std; #define INF 1e16 #defin ...

  4. 2017-2018 ACM-ICPC Pacific Northwest Regional Contest (Div. 1) B - Enlarging Enthusiasm dp好题

    B - Enlarging Enthusiasm 感觉做到过好多的dp题都会和单调性结合在一起. 思路:dp[ s ][ pre ][ res ] 表示的是已选择了s,上一个是pre, 还有res 的 ...

  5. C++ 基础 杂类

    1.set:  基本上跟map是相同(只有一个键),set是key-value 放在一起,map 是分开的,既然都加key ,所以set<> 的内容不可能有重复的情况出现 example: ...

  6. linux config

    1.自定义命令:.bashrc 文件添加自定义命令 2.完成后,命令:source . .bashrc  使配置的自定义命令生效,如果提示没有该命令的情况,用   . .bashrc (注意是2个“点 ...

  7. [python]一个关于默认参数的老问题和一个有关优化的新问题

    一个老问题: def func(defau=[]): defau.append(1) return defau print(func())#print[1] print(func())#print[1 ...

  8. vsftp 虚拟用户+MySQL认证独立家目录

    centos7 系统 安装包 yum -y install mariadb vsftpd openssl-devel  mysql-devel  pam-devel yum -y groupinsta ...

  9. python opencv3 grabcut前景检测

    git:https://github.com/linyi0604/Computer-Vision import numpy as np import cv2 import matplotlib.pyp ...

  10. DataTable,List,Dictonary互转,筛选及相关写法

    1.创建自定义DataTable  /// 创建自定义DataTable(一) 根据列名字符串数组, /// </summary> /// <param name="sLi ...