NSTimer、GCD、CADisplay

销毁情况

#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];
}

// 返回不会销毁,默认添加到runloop中了
- (void)nstime2
{
//    自动添加到runlooploop的默认人模式  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];
}

// 返回会销毁,默认添加到runloop中了
- (void)gcdtimer
{
    __weak typeof(self) wSelf = self;
//   timer  一定要被设置为成员变量 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);  会造成内存泄漏
    });
//   激活源对象
//    dispatch_resume(self.timer);

/*
 dispatch_resume()   启用
 dispatch_suspend()  暂停, 当前正在执行的需要执行完才会暂停,暂停的 timer不能被释放,会引起崩溃
 dispatch_source_cancel() 销毁
 */
}
//1、但remuse(恢复)的状态下,如果再进行一次resume(恢复)就会crash,所以要注册一个BOOL值的状态进行记录,防止多次suspend和resume引起闪退。
//2、在suspend(暂停)的状态下,如果你设置timer = nil就会crash。
//3、在suspend(暂停)的状态下,即使调用dispatch_source_cancel也没用,会造成内存泄漏,甚至崩溃。

// 返回不会销毁,需要手动添加到runloop中
- (void)displaylink
{
//   屏幕刷新时间, 每秒60次,  16.7ms一次
//    不会马上执行,需要添加到 runloop中
    CADisplayLink *display = [CADisplayLink displayLinkWithTarget:self selector:@selector(run)];
    [display addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}

- (void)run
{
    NSLog(@"run...");
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    dispatch_suspend(self.timer);
}

- (void)dealloc
{
    NSLog(@"SecondViewController deallc");
}

@end

你可能感兴趣的:(NSTimer、GCD、CADisplay)