iOS开发 NSTimer 循环引用解决几种方案 oc代码
在iOS开发中,使用NSTimer时,经常会遇到循环引用的问题。以下是几种解决方案:
- 使用weak引用:在NSTimer的回调方法中,使用weak引用来引用self,防止循环引用。示例代码如下:
__weak typeof(self) weakSelf = self;
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:weakSelf selector:@selector(timerFired:) userInfo:nil repeats:YES];
- 使用block:将NSTimer的回调方法包装成block,然后在block内部使用weak引用来引用self。示例代码如下:
__weak typeof(self) weakSelf = self;
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 block:^{
[weakSelf timerFired:timer];
} repeats:YES];
- 使用GCD的定时器:使用dispatch_source_t创建一个GCD的定时器,然后在block内部使用weak引用来引用self。示例代码如下:
__weak typeof(self) weakSelf = self;
dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0);
dispatch_source_set_event_handler(timer, ^{
[weakSelf timerFired];
});
dispatch_resume(timer);
- 使用CADisplayLink:CADisplayLink是一个和屏幕刷新频率相同的定时器,可以用来实现每帧都执行的操作。在CADisplayLink的回调方法中,使用weak引用来引用self。示例代码如下:
__weak typeof(self) weakSelf = self;
CADisplayLink *displayLink = [CADisplayLink displayLinkWithTarget:weakSelf selector:@selector(timerFired:)];
[displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
无论使用哪种方法,都需要注意在适当的时候停止或者销毁NSTimer,以避免内存泄漏。
原文地址: http://www.cveoy.top/t/topic/jbsX 著作权归作者所有。请勿转载和采集!