NSTime避免循环引用须知

-(void)viewWillDisappear:(BOOL)animated{
    
    // 注意关闭 timer 不然因为循环引用(self 引用myTimer,同时myTimer引用self)不会执行dealloc 
    [self endMyTimer];
}


#pragma mark - UIWebViewDelegate
- (void)webViewDidStartLoad:(UIWebView *)webView {
   
    // 启动myTimer
    [self startMyTimer];
    
}



- (void)startMyTimer{
    if (myTimer) {
        [myTimer invalidate];
        myTimer = nil;
    }
    //0.01667 is roughly 1/60, so it will update at 60 FPS
    myTimer = [NSTimer scheduledTimerWithTimeInterval:0.01667 target:self selector:@selector(timerCallback) userInfo:nil repeats:YES];
    [myTimer fire];
}

#pragma mark - 终止myTimer
- (void)endMyTimer{
    if (myTimer) {
        [myTimer invalidate];
        myTimer = nil;
    }
}

参考:
1、http://www.codecate.com/code/archives/77

你可能感兴趣的:(NSTime避免循环引用须知)