NSTimer使用時處理不好容易造成循環(huán)引用,導(dǎo)致控制器無法正常釋放。之前使用的方法或多或少都會在某些特定環(huán)境下造成一些問題。今天正好學(xué)習(xí)到了3種解決循環(huán)引用的方法,在這里記錄一下。
1.在didMoveToParentViewController:中取消NSTimer強引用
- (void)didMoveToParentViewController:(UIViewController *)parent{
if (!parent) {
[self.timer invalidate];
self.timer = nil;
}
}
2.利用target屬性和class_addMethod,使NSTimer不直接強引用self
@property (nonatomic , strong) id target;
_target = [NSObject new];
class_addMethod([_target class], @selector(update), (IMP)update, "v@:");
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:_target selector:@selector(update) userInfo:nil repeats:YES];
void update(id self,SEL _cmd)
{
NSLog(@"class_addMethod ----> update");
}
- (void)dealloc
{
[self.timer invalidate];
self.timer = nil;
}
3.利用NSProxy類(推薦)
在YYText中就使用到了YYTextWeakProxy作為代理中間類。有興趣的可以去看看
//DEProxy.h
@interface DEProxy : NSProxy
@property (nonatomic , weak) id target;
@end
//DEProxy.m
@implementation DEProxy
//1.1.標(biāo)準(zhǔn)轉(zhuǎn)發(fā)-簽名
- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel
{
return [_target methodSignatureForSelector:sel];
}
//1.2.標(biāo)準(zhǔn)轉(zhuǎn)發(fā)-轉(zhuǎn)發(fā)
- (void)forwardInvocation:(NSInvocation *)invocation{
[invocation invokeWithTarget:_target];
}
//2.快速轉(zhuǎn)發(fā)
//- (id)forwardingTargetForSelector:(SEL)selector {
// return _target;
//}
@end
//使用
@property (nonatomic , strong) DEProxy *targetProxy;
_targetProxy = [DEProxy alloc];
_targetProxy.target = self;
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:_targetProxy selector:@selector(update) userInfo:nil repeats:YES];
-(void)update{
NSLog(@"update");
}
- (void)dealloc
{
[self.timer invalidate];
self.timer = nil;
}
4.在iOS10以后,NSTimer新增了block方法來解決循環(huán)引用問題
if (@available(iOS 10.0, *)) {
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 repeats:YES block:^(NSTimer * _Nonnull timer) {
NSLog(@"update");
}];
} else {
// Fallback on earlier versions
}
- (void)dealloc
{
[self.timer invalidate];
self.timer = nil;
}