弱引用NSTimer对象
来源:互联网 发布:echo linux 写入文件 编辑:程序博客网 时间:2024/04/30 09:53
不要忘记 NSTimer 会 retain它的对象,意味着如果你的timer的 target 也是 NSTimer的 拥有者的时候,很容易就进入一个循环引用。
你可以通过一个 代理的delegate来 绕过这个, 这个代理对象拥有这个真正target的 weak 引用。
@interface TMWeakTimerTarget :NSObject
- (instancetype)initWithTarget:(id)target selector:(SEL)selector;
- (void)timerDidFire:(NSTimer *)timer;
@end
@interface TMWeakTimerTarget()
@property (nonatomic,weak) id target;
@property (nonatomic) SEL selector;
@end
@implementationTMWeakTimerTarget
#pragma mark - Initialization
- (instancetype)initWithTarget:(id)target selector:(SEL)selector {
if (self = [super init]) {
_target = target;
_selector = selector;
}
return self;
}
- (id)init {
return [self initWithTarget:nil selector:nil];
}
#pragma mark - TMWeakTimerTarget
- (void)timerDidFire:(NSTimer *)timer {
if (self.target) {
// `performSelector:` would generate a potential leak warning (http://stackoverflow.com/a/7073761/503916)
objc_msgSend(self.target,self.selector);
}
else {
[timer invalidate];
}
}
@end
你可以创建一个 NSTimer的 Category ,来更容易的使用这个类
/**
* Method that can be used in favor of `scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:` in case the target retains the timer instance. Using the aforementioned method would create a retain cycle in this case.
*/
@interface NSTimer (RetainCyclePrevention)
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)timeInterval
weakTarget:(id)target
selector:(SEL)selector
userInfo:(id)userInfo
repeats:(BOOL)yesOrNo;
@end
@implementationNSTimer (RetainCyclePrevention)
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)timeInterval
weakTarget:(id)target
selector:(SEL)selector
userInfo:(id)userInfo
repeats:(BOOL)yesOrNo {
return [self scheduledTimerWithTimeInterval:timeInterval
target:[[TMWeakTimerTarget alloc] initWithTarget:target
selector:selector]
selector:@selector(timerDidFire:)
userInfo:userInfo
repeats:yesOrNo];
}
@end
- 弱引用NSTimer对象
- 防止NSTimer和调用对象之间的循环引用
- 防止NSTimer和调用对象之间的循环引用
- NSTimer解除循环引用
- NSTimer中的循环引用
- NSTimer的循环引用
- NSTimer和实现弱引用的timer的方式
- NSTimer和实现弱引用的timer的方式
- NSTimer循环引用的成因
- NSTimer循环引用的问题
- NSTimer的循环引用问题
- ios NSTimer引起的循环引用,以及NSTimer的使用
- NSTimer造成循环引用的问题
- 警惕使用NSTimer时的循环引用
- NSTimer循环引用不释放问题
- iOS - 关于NSTimer的循环引用
- iOS 中的 NSTimer 强引用与释放
- iOS | 小心NSTimer中的循环引用
- 设计模式学习--桥接模式
- 2015070904 - 阅读后的书籍有着落了
- leetCode 40.Combination Sum II(组合总和II) 解题思路和方法
- 1985年公布的C++语言1.0版的内容中又添加了一些重要特征
- API其他函数 ,system date,..
- 弱引用NSTimer对象
- Java.Lang.NoSuchMethod 错误
- C#基础学习需要注意的几个知识点
- jsp(六) setProperty getProperty
- Java学习笔记--变量
- MYSQL学习笔记(三)
- 2015070905 - 仙剑六
- Java学习笔记--数据类型
- Invert Binary Tree