2012-11-27 31 views
0

我有一個計時器,用於更新我的程序中的標籤,並且當有用戶交互(如手指停止或滾動)發生時,計時器暫停。我使用這段代碼來修復它,並且它可以工作,但它似乎正在重新創建內存分配,導致遊戲在幾分鐘內崩潰。導致2GB內存分配的NSDate

-(void) timeChangerInitNormal { 
    if (running == YES) 
     if (_timerQueue) return; 
    _timerQueue = dispatch_queue_create("timer queue", 0); 

    dispatch_async(_timerQueue, ^{ 

     timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(timeChanger) userInfo:nil repeats:YES]; 

     while ([timer isValid]) { 
      [[NSRunLoop currentRunLoop] runUntilDate:[[NSDate date] dateByAddingTimeInterval:5.0]]; 
     } 
    }); 
} 


- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView; 
{ 
    [self _lockInteraction]; 
} 

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate; 
{ 
    [self _unlockInteraction]; 
} 
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view 
{ 
    [self _lockInteraction];  
} 
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale; 
{ 
    [self _unlockInteraction]; 
} 

- (void) _lockInteraction 
{ 
    [self _setControls:_staticViews interacted:NO]; 
    [self _setControls:_autoLayoutViews interacted:NO]; 
} 

- (void) _unlockInteraction 
{ 
    [self _setControls:_staticViews interacted:YES]; 
    [self _setControls:_autoLayoutViews interacted:YES]; 
} 

- (void) _setControls:(NSArray *)controls interacted:(BOOL)interacted 
{ 
    for (UIControl *control in controls) { 
     if ([control isKindOfClass:[UIControl class]]) { 
      control.userInteractionEnabled = interacted; 
     } 
    } 
} 

- (void)scrollViewDidZoom:(UIScrollView *)scrollView 
{ 
    [self _updatePositionForViews:_autoLayoutViews]; 
} 

- (void)scrollViewDidScroll:(UIScrollView *)scrollView 
{ 
    [self _updatePositionForViews:_autoLayoutViews]; 
} 

- (UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView; 
{ 
    return _mapImageView; 
} 
+0

您是否在使用ARC? –

+0

是的,我正在使用ARC。 – PappaSmalls

+0

你在哪種iOS設備上擁有2GB的RAM? – 2012-11-27 19:46:21

回答

1

TL;博士

你只是想用一個普通的定時器。創建它,它添加到事件跟蹤模式後:

[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSEventTrackingRunLoopMode]; 

這將使而跟蹤事件觸發它(觸落於滑塊,滾動視圖,...)。

如果你仍然想知道:你創建了一個基本上保持CPU在while循環中旋轉的線程,創建了自動發佈的NSDate對象。由於沒有內部自動釋放池,日期對象沒有被釋放。

但是不要試圖修復你舊的代碼。還有更多的問題。

相關問題