銷毀情況
#import "SecondViewController.h"
@interface SecondViewController ()
@property (nonatomic, strong) dispatch_source_t timer;
@end
@implementation SecondViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor grayColor];
// [self gcdtimer];
// [self nstimer];
// [self nstime2];
[self nstime3];
// [self displaylink];
}
// 返回不會銷毀,需要手動添加到runloop中
- (void)nstimer
{
NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(run) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
}
// 返回不會銷毀,默認(rèn)添加到runloop中了
- (void)nstime2
{
// 自動添加到runlooploop的默認(rèn)人模式 NSDefaultRunLoopMode
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(run) userInfo:nil repeats:YES];
}
// 返回不會銷毀,需要手動添加到runloop中
- (void)nstime3
{
NSDate *fireDate = [[NSDate alloc] initWithTimeIntervalSinceNow:5]; // 5s后開啟定時器
NSTimer *timer = [[NSTimer alloc] initWithFireDate:fireDate interval:1 target:self selector:@selector(run) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
}
// 返回會銷毀,默認(rèn)添加到runloop中了
- (void)gcdtimer
{
__weak typeof(self) wSelf = self;
// timer 一定要被設(shè)置為成員變量 dispatch_source_t ,否則將會立即被釋放
self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
dispatch_source_set_timer(self.timer, dispatch_walltime(NULL, 0), (uint64_t)(1*NSEC_PER_SEC), 0);
dispatch_source_set_event_handler(self.timer, ^{
// [self run]; 不會銷毀
[wSelf run];
// dispatch_resume(self.timer); 會造成內(nèi)存泄漏
});
// 激活源對象
// dispatch_resume(self.timer);
/*
dispatch_resume() 啟用
dispatch_suspend() 暫停, 當(dāng)前正在執(zhí)行的需要執(zhí)行完才會暫停,暫停的 timer不能被釋放,會引起崩潰
dispatch_source_cancel() 銷毀
*/
}
//1、但remuse(恢復(fù))的狀態(tài)下,如果再進(jìn)行一次resume(恢復(fù))就會crash,所以要注冊一個BOOL值的狀態(tài)進(jìn)行記錄,防止多次suspend和resume引起閃退。
//2、在suspend(暫停)的狀態(tài)下,如果你設(shè)置timer = nil就會crash。
//3、在suspend(暫停)的狀態(tài)下,即使調(diào)用dispatch_source_cancel也沒用,會造成內(nèi)存泄漏,甚至崩潰。
// 返回不會銷毀,需要手動添加到runloop中
- (void)displaylink
{
// 屏幕刷新時間, 每秒60次, 16.7ms一次
// 不會馬上執(zhí)行,需要添加到 runloop中
CADisplayLink *display = [CADisplayLink displayLinkWithTarget:self selector:@selector(run)];
[display addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}
- (void)run
{
NSLog(@"run...");
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
dispatch_suspend(self.timer);
}
- (void)dealloc
{
NSLog(@"SecondViewController deallc");
}
@end