objective-cnstimernstableviewnswindowawakefromnib

Pause and resume repeatable NSTimer when closing and opening window


Now I know there are questions on SO about NSTimer pause, but none on repeatable and closing/opening windows. Toggeling pause works perfect (checked using button). Reopening window gives BAD_ACCESS error.

- (void)awakeFromNib
{
    self.timer = [[NSTimer scheduledTimerWithTimeInterval:0.85
                                               target:self
                                             selector:@selector(populateTable:)
                                             userInfo:nil
                                              repeats:YES] retain];
}

-(IBAction)fireUpTableWindow:(id)sender
{
    [aTableWindow makeKeyAndOrderFront:self];
    [self resumeTimer:timer];
}


NSDate *pauseStart, *previousFireDate;

-(void) pauseTimer:timer
{
    pauseStart = [[NSDate dateWithTimeIntervalSinceNow:0] retain];    
    previousFireDate = [[timer fireDate] retain];
    [timer setFireDate:[NSDate distantFuture]];
}

-(void) resumeTimer:timer
{
    float pauseTime = -1*[pauseStart timeIntervalSinceNow];
    [timer setFireDate:[previousFireDate initWithTimeInterval:pauseTime sinceDate:previousFireDate]];
    [pauseStart release];
    [previousFireDate release];
}

- (IBAction)closeTableWindow:(id)sender
{
    [self pauseTimer:timer];
    [self->aTableWindow close];
    [self release];
}
}

Solution

  • Reopening window gives BAD_ACCESS error.

    Check in Interface Builder that the Window is not set to Release on Close.