内存管理-定时器(CADisplayLink、NSTimer)

使用CADisplayLink、NSTimer有什么需要注意的?

一、创建定时器

@property (strong, nonatomic) NSTimer *timer;
@property (strong, nonatomic) CADisplayLink *link;

CADisplayLink需要加到RunLoop中才能使用

 self.link = [CADisplayLink displayLinkWithTarget:self selector:@selector(linktest)];
 [self.link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
- (void)linktest{
    NSLog(@"%s", __func__);
}
- (void)dealloc
{
    [self.link invalidate];
    NSLog(@"%s", __func__);
}

运行结果:一直在调用,即使页面摧毁也在调用

2021-05-26 16:19:13.167971+0800 Interview03-定时器[32664:4448900] -[ViewController linktest]
2021-05-26 16:19:13.184755+0800 Interview03-定时器[32664:4448900] -[ViewController linktest]
2021-05-26 16:19:13.201062+0800 Interview03-定时器[32664:4448900] -[ViewController linktest]
2021-05-26 16:19:13.217795+0800 Interview03-定时器[32664:4448900] -[ViewController linktest]
2021-05-26 16:19:13.234851+0800 Interview03-定时器[32664:4448900] -[ViewController linktest]

小结:造成循环引用

原因:self对CADisplayLink强引用,CADisplayLink对target强引用,target对self强引用,造成循环引用,NSTimer也有类似问题
NSTimer的scheduledTimerWithTimeInterval创建方式会造成循环引用

self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerTest) userInfo:nil repeats:YES];
- (void)timerTest
{
    NSLog(@"%s", __func__);
}
- (void)dealloc
{
    [self.timer invalidate];
    NSLog(@"%s", __func__);
}

二、问题抛出

NSTimer的这种scheduledTimerWithTimeInterval创建方式以及CADisplayLink的displayLinkWithTarget创建方式易造成循环引用

三、解决办法

1、尝试_ _weak

  __weak typeof (self) weakself = self;
 self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:weakself selector:@selector(timerTest) userInfo:nil repeats:YES];

循环引用问题仍旧存在

分析原因:
往常的能解决是因为在block中 此处传参给taget 传进去的都是指针 强弱引用都没区别

2、使用block 弱引用

__weak typeof (self) weakself = self;
 self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 repeats:YES block:^(NSTimer * _Nonnull timer) {
 [weakself timerTest];
 }];

self对NSTimer强引用,NSTimer对block强引用 block对self产生弱引用weakself

3、使用代理对象

引一个弱引用

3.1继承NSObject
.h文件中

@interface MJProxy1 : NSObject
+ (instancetype)proxyWithTarget:(id)target;
@property (weak, nonatomic) id target;//弱引用
@end

.m文件中

@implementation MJProxy1

+ (instancetype)proxyWithTarget:(id)target
{
    MJProxy1 *proxy = [[MJProxy1 alloc] init];
    proxy.target = target;
    return proxy;
}

//消息转发三种
- (id)forwardingTargetForSelector:(SEL)aSelector
{
    return self.target;
}

调用

 self.link = [CADisplayLink displayLinkWithTarget:[MJProxy1 proxyWithTarget:self] selector:@selector(linktest)];
[self.link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:[MJProxy1 proxyWithTarget:self] selector:@selector(timerTest) userInfo:nil repeats:YES];

不再循环引用
3.1继承NSProxy
.h文件中

@interface MJProxy : NSProxy
+ (instancetype)proxyWithTarget:(id)target;
@property (weak, nonatomic) id target;
@end

.m文件中

@implementation MJProxy

+ (instancetype)proxyWithTarget:(id)target
{
    // NSProxy对象不需要调用init,因为它本来就没有init方法
    MJProxy *proxy = [MJProxy alloc]; 
    proxy.target = target;
    return proxy;
}

//消息转发
//只要调用MJProxy的某个方法就马上调用他的另一个方法methodSignatureForSelector
- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel
{
    return [self.target methodSignatureForSelector:sel];
}


//消息转发三种 调用不到时先调用这个
- (void)forwardInvocation:(NSInvocation *)invocation
{
    [invocation invokeWithTarget:self.target];
}
//- (id)forwardingTargetForSelector:(SEL)aSelector  NSProxy没有这个方法

调用

 self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:[MJProxy proxyWithTarget:self] selector:@selector(timerTest) userInfo:nil repeats:YES];
self.link = [CADisplayLink displayLinkWithTarget:[MJProxy proxyWithTarget:self] selector:@selector(linktest)];
[self.link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];

同样问题解决,不再循环引用

三、总结

1、解决问题的宗旨,使用代理对象将强引用转为若引用,并用消息转发机制对找不到响应的方法进行转发
2、 NSProxy 、NSObject 同为基类,不同点: NSProxy方法不存在时专门用来消息转发直接在自己类里搜索节省时间NSObject多了一步从父类里搜索方法的过程
3、定时器依赖于runloop去实现的,有可能并不准时,如果定时器是设置每隔0.5秒,在runloop中 跑完一圈去看一次定时器到没有到0.5,到了就执行定时器方法,不到会继续跑圈执行别的任务,但每次循环时间不确定,有可能一圈任务多的时候超过0.5间隔时间导致不准确,所以项目中想用定时器最好用GCD,基于内核比较准时

gcd简单的定时器

@property(nonatomic,strong)dispatch_source_t gcdTimer;
//0.创建一个队列
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);

    //1.创建一个GCD的定时器
    /*
     第一个参数:说明这是一个定时器
     第四个参数:GCD的回调任务添加到那个队列中执行,如果是主队列则在主线程执行
     */
    dispatch_source_t gcdTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);

    //2.设置定时器的开始时间,间隔时间以及精准度
    //设置开始时间,三秒钟之后调用,注:GCD中的时间为纳秒NSEC_PER_SEC,3.0 *NSEC_PER_SEC即为3秒
    dispatch_time_t start = dispatch_time(DISPATCH_TIME_NOW,3.0 *NSEC_PER_SEC);
    //设置定时器工作的间隔时间
    uint64_t intevel = 1.0 * NSEC_PER_SEC;

    /*
     第一个参数:要给哪个定时器设置
     第二个参数:定时器的开始时间DISPATCH_TIME_NOW表示从当前开始
     第三个参数:定时器调用方法的间隔时间
     第四个参数:定时器的精准度,如果传0则表示采用最精准的方式计算,如果传大于0的数值,则表示该定时切换可以接收该值范围内的误差,通常传0
     该参数的意义:可以适当的提高程序的性能
     注意点:GCD定时器中的时间以纳秒为单位(面试)
     */
    dispatch_source_set_timer(gcdTimer, start, intevel, 0 * NSEC_PER_SEC);
    //3.设置定时器开启后回调的方法
    /*
     第一个参数:要给哪个定时器设置
     第二个参数:回调block
     */
    dispatch_source_set_event_handler(gcdTimer, ^{
        NSLog(@"------%@",[NSThread currentThread]);
    });

    //4.执行定时器
    dispatch_resume(gcdTimer);
 //注意:dispatch_source_t本质上是OC类,在这里是个局部变量,需要强引用
    self.gcdTimer = gcdTimer;

运行结果:

2021-05-26 17:13:10.538833+0800 Interview03-定时器[32962:4500199] ------<NSThread: 0x600001e4c6c0>{number = 8, name = (null)}
2021-05-26 17:13:11.538501+0800 Interview03-定时器[32962:4500201] ------<NSThread: 0x600001e6cb00>{number = 5, name = (null)}
2021-05-26 17:13:12.537677+0800 Interview03-定时器[32962:4500201] ------<NSThread: 0x600001e6cb00>{number = 5, name = (null)}
2021-05-26 17:13:12.783975+0800 Interview03-定时器[32962:4500102] -[ViewController dealloc]

感兴趣的可以自行封装方便调用

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,440评论 5 467
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,814评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,427评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,710评论 1 270
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,625评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,014评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,511评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,162评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,311评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,262评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,278评论 1 328
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,989评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,583评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,664评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,904评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,274评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,856评论 2 339

推荐阅读更多精彩内容