防止 NSTimer retain 作为 target 的 self

先吐槽一下这个标题,空格略蛋疼,不像中文,但是不写空格看上去则更诡异,求解决方案……

NSTimer会retain它的target,这样如果在控制器当中定义一个NSTimer,target指定为self,则会引起循环引用。

解决方案和防止block引用self一样,第一步需要把NSTimer的操作封装到一个block里,第二步则需要传递一个self的弱引用给block。

首先定义一个NSTimer的分类:

 1 #import <Foundation/Foundation.h>
 2
 3 @interface NSTimer (BlockSupport)
 4
 5 + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)interval block:(void(^)())block repeats:(BOOL)repeats;
 6
 7 @end
 8
 9 #import "NSTimer+BlockSupport.h"
10
11 @implementation NSTimer (BlockSupport)
12
13 + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)interval block:(void(^)())block repeats:(BOOL)repeats {
14     return [self scheduledTimerWithTimeInterval:interval target:self selector:@selector(blockInvoke:) userInfo:[block copy] repeats:repeats];
15 }
16
17 + (void)blockInvoke:(NSTimer *)timer {
18     void (^block)() = timer.userInfo;
19     if (block) {
20         block();
21     }
22 }
23
24 @end

这个分类支持使用Block创建NSTimer,把操作传递到了万能对象userInfo里面,之后在控制器当中以这样的方式创建:

1     __block typeof(self) weakSelf = self;
2     _timer = [NSTimer scheduledTimerWithTimeInterval:0.5 block:^{
3         [weakSelf doSth];
4     } repeats:YES];

如此一来,NSTimer就不会令控制器的引用计数+1了。

防止 NSTimer retain 作为 target 的 self

时间: 2024-12-12 12:23:47

防止 NSTimer retain 作为 target 的 self的相关文章

NSTimer retain了它的target

今天一直在释放一个类对象B(这个类中有一个timer),根据arc的原理来说, B的retainCount为0时,就会调用dealloc.但是当为我把B=nil,没有进入dealloc. -(void)dealloc { [timer invalidate]; timer = nil; } 后来经过朋友提醒知道,nstimer对B类有引用,也就是在初始化时对target有retain. timer = [NSTimer scheduledTimerWithTimeInterval:1.0 sel

iOS开发——实用技术OC篇&amp;NSTimer使用注意点及总结

NSTimer使用注意点及总结 总结以下在NSTimer的使用中遇到的一些问题: 1. 不要在dealloc函数中停止并释放NSTimer 如果这样做,会导致对象永远无法调用dealloc函数,也就是会造成内存泄漏. 一个比较合理的解释是NSTimer的回调方法具有retain属性,所以不停止它的情况下被引用对象的retainCount无法降为0,导致内存泄漏的死循环 2.因为要实现类似视频软件里面,UIScrollview定时循环滑动,用到了NSTimer类.在特定时事件情况下需要暂停,和重新

iOS容易造成循环引用的三种场景NSTimer以及对应的使用方法(一)

NSTimer A timer provides a way to perform a delayed action or a periodic action. The timer waits until a certain time interval has elapsed and then fires, sending a specified message to a specified object(timer就是一个能在从现在开始的未来的某一个时刻又或者周期性的执行我们指定的方法的对象)

【转】IOS NSTimer 定时器用法总结

原文网址:http://my.oschina.net/u/2340880/blog/398598 NSTimer在IOS开发中会经常用到,尤其是小型游戏,然而对于初学者时常会注意不到其中的内存释放问题,将其基本用法总结如下: 一.初始化方法:有五种初始化方法,分别是 + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo; -

iOS 中的 NSTimer

iOS 中的 NSTimer NSTimer fire 我们先用 NSTimer 来做个简单的计时器,每隔5秒钟在控制台输出 Fire .比较想当然的做法是这样的: @interface DetailViewController () @property (nonatomic, weak) NSTimer *timer; @end @implementation DetailViewController - (IBAction)fireButtonPressed:(id)sender { _ti

小结OC中Retain cycle(循环引用)

retain cycle 的产生 说到retain cycle,首先要提一下Objective-C的内存管理机制. 作为C语言的超集,Objective-C延续了C语言中手动管理内存的方式,但是区别于C++的极其非人道的内存管理,Objective-C提出了一些机制来减少内存管理的难度. 比如:内存计数. 在 Objective-C中,凡是继承自NSObject的类都提供了两种方法,retain和release.当我们调用一个对象的retain时,这个 对象的内存计数加1,反之,当我们调用rel

初识iOS NSTimer 循环引用不释放问题

原文转自 :http://www.codecate.com/code/?p=77 最近开发遇到NSTimer Target 造成循环引用问题,不释放,以下是解决方案. stackoverflow上的一个解决方案 http://stackoverflow.com/questions/16821736/weak-reference-to-nstimer-target-to-prevent-retain-cycle 原文如下 Weak Reference to NSTimer Target To Pr

NSTimer和Runloop的关系

什么是NSTimer 官方给出解释是:“A timer provides a way to perform a delayed action or a periodic action. The timer waits until a certain time interval has elapsed and then fires, sending a specified message to a specified object. ” 翻译过来就是timer就是一个能在从现在开始的后面的某一个时

关于NSRunLoop和NSTimer的深入理解

一.什么是NSRunLoop NSRunLoop是消息机制的处理模式 NSRunLoop的作用在于有事情做的时候使的当前NSRunLoop的线程工作,没有事情做让当前NSRunLoop的线程休眠 NSTimer默认添加到当前NSRunLoop中,也可以手动制定添加到自己新建的NSRunLoop NSRunLoop就是一直在循环检测,从线程start到线程end,检测inputsource(如点击,双击等操作)同步事件,检测timesource同步事件,检测到输入源会执行处理函数,首先会产生通知,