NSTimer注意内存泄露(真该死)
2015-09-23 16:24
246 查看
NSTimer可以用来执行一些定时任务,比较常用的方法就是:
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;
可是,仔细看官方文档中对于参数target的说明,可以看到这样一段:
target
The object to which to send the message specified by aSelector when the timer fires. The timer maintains a strong reference to this object until it (the timer) is invalidated.
也就是说,NSTimer会强引用target.
那么如果我们按照通常的用法,在view controller中创建一个timer
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:YES];
就会带来循环引用,viewController的dealloc()方法不会正确调用,从而导致内存泄漏。
所以正确的做法,如果是在view controller中持有了NSTimer的对象,那么需要确保在view controller关闭之前,执行如下方法:
[timer invalidate];
有些人推荐是在viewDidDisappear的时候调用这个方法,但在部分业务逻辑中,跳转到下一个页面的时候并不是一定需要停止这个定时器的;所以需要根据自己的需要来调整。
另外,多说一句,看到有些代码中使用timer的时候,定义的selector都没有定义参数的,然后根据官方文档,
The selector should have the following signature: timerFireMethod: (including a colon to indicate that the method takes an argument). The timer passes itself as the argument, thus the method would adopt the following pattern:
- (void)timerFireMethod:(NSTimer *)timer;
也就是说,这个selelctor实际上是需要带上timer作为参数的。
联想到上一篇谈到NSNotificationCenter 的文章,
- (void)addObserver:(id)observer selector:(SEL)aSelector name:(NSString *)aName object:(id)anObject;
里面对于selector其实也说得很清楚,也是需要带上一个参数而且只能带上一个NSNotification的对象作为参数的.
notificationSelector
Selector that specifies the message the receiver sends notificationObserver to notify it of the notification posting. The method specified by notificationSelector must have one and only one argument (an instance of NSNotification).
The End.
原文:http://www.cnblogs.com/agger0207/p/4419348.html
NSTimer 会强持有所在的 target
相关文章推荐
- SQL工具类
- mssqlserver修改表名,列名,添加表列,删除表列,修改表列类型
- x11vnc 安装与配置(转)
- PAT(甲级)1010
- FFmpeg demuxing api 中文文档
- Maven的配置文件pom.xml
- kvo使用方法简述
- WebLogic - Enabling Auto Login by Using the Boot Identity File
- linux dd 测试硬盘的读写性能
- LeetCode---Invert Binary Tree
- poj3126 bfs
- Java实现对MongoDB的AND、OR和IN操作
- ThinkPHP3.2中关联查询的代码收藏
- 原码,补码,反码的学习
- php常用的安全过滤函数
- EF code first 生成edmx文件
- linux bin sbin目录
- PAT(甲级)1009
- 错误汇总~持续更新中
- Maximal Rectangle 二维矩形中最大的1个数