I have a timer that updates labels in my program and the timer pauses when ever there a user interaction such as finger down or scrolling occurs. I’m using this code to fix it and it works, but it seems to be re-creating memory allocations causing the game to crash within minutes.
-(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;
}
tl;dr
You just want to use a normal timer. After creating it add it to the event tracking mode:
This will make it trigger while tracking events (touch down on slider, scroll view, …).
In case you still want to know: You created a thread that basically kept a CPU busy spinning in the while loop, creating autoreleased
NSDateobjects. Since there was no inner autorelease pool the date objects were not released.But don’t try to fix you old code. There are more issues.