ARC模式下delloc()注意事项
------------------------------------------------------------------------------------------------------------------------------------------------
一. 有一次面试问到:从VC1 推到VC2会不会调用dealloc呢?答案是不会
而从VC2 dismiss 会调VC2的dealloc 不会调用VC1的dealloc方法 有代码为证:
#import "ViewController.h" #import "ViewController2.h" @interface ViewController () @end @implementation ViewController - (void)dealloc { NSLog(@"ViewController dealloc"); } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (IBAction)goVC2:(id)sender { ViewController2 *vc2 = [ViewController2 new]; [self presentViewController:vc2 animated:YES completion:^{ }]; } @end
#import "ViewController2.h" @interface ViewController2 () @end @implementation ViewController2 - (void)dealloc { NSLog(@"ViewController2 dealloc"); } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. }
二. ViewController中被加入了定时器,而没有及时的invalidate ,会怎样?
#import "ViewController.h" #import "ViewController2.h" @interface ViewController () { NSTimer *timer; } @end @implementation ViewController - (void)dealloc { NSLog(@"ViewController dealloc %@",self); } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(updateTime:) userInfo:nil repeats:YES]; } -(void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [timer invalidate]; } -(void)updateTime:(id)sender { NSLog(@"ViewController updateTime"); } - (IBAction)goVC2:(id)sender { ViewController2 *vc2 = [ViewController2 new]; [self presentViewController:vc2 animated:YES completion:^{ }]; } @end
没调 [timer invalidate]; 打印
2020-02-29 17:23:04.608616+0800 YiJTApp[17656:482667] ViewController updateTime 2020-02-29 17:23:07.608558+0800 YiJTApp[17656:482667] ViewController updateTime 2020-02-29 17:23:10.607419+0800 YiJTApp[17656:482667] ViewController updateTime 2020-02-29 17:23:13.608551+0800 YiJTApp[17656:482667] ViewController updateTime 2020-02-29 17:23:16.608406+0800 YiJTApp[17656:482667] ViewController updateTime 2020-02-29 17:23:19.608554+0800 YiJTApp[17656:482667] ViewController updateTime 2020-02-29 17:23:22.608535+0800 YiJTApp[17656:482667] ViewController updateTime 2020-02-29 17:23:25.409616+0800 YiJTApp[17656:482667] ViewController2 dealloc 2020-02-29 17:23:25.607751+0800 YiJTApp[17656:482667] ViewController updateTime 2020-02-29 17:23:28.608624+0800 YiJTApp[17656:482667] ViewController updateTime 2020-02-29 17:23:31.607510+0800 YiJTApp[17656:482667] ViewController updateTime
调 [timer invalidate]; 打印
2020-02-29 17:26:12.271080+0800 YiJTApp[17763:485905] ViewController updateTime 2020-02-29 17:26:23.268043+0800 YiJTApp[17763:485905] ViewController2 dealloc
不管是有定时器,或者被代理,block强引用。VC1都不会调delloc方法。
此文仅为鄙人学习笔记之用,朋友你来了,如有不明白或者建议又或者想给我指点一二,请私信我。liuw_flexi@163.com/QQ群:582039935.
我的gitHub: (学习代码都在gitHub)
https://github.com/nwgdegitHub/