Skip to content


Subversion checkout URL

You can clone with
Download ZIP
A category on NSTimer to fix behavior with background execution
Objective-C C
Branch: master
Failed to load latest commit information.
NSTimer+AbsoluteFireDate.h Add license
NSTimer+AbsoluteFireDate.m Add license Initial commit

This category works around some surprising NSTimer behavior on iOS.

You would expect that the initWithFireDate:interval:target:selector:userInfo:repeats: method of NSTimer would create a timer that fires at, or after, the specified date. Not so on iOS, specifically with regard to background execution.

Suppose you create a timer that fires five minutes in the future:

NSDate *d = [NSDate dateWithTimeIntervalSinceNow:60*5];
NSTimer *t = [[NSTimer alloc] initWithFireDate:d 
[[NSRunLoop mainRunLoop] addTimer:t forMode:NSDefaultRunLoopMode];

The user runs your app at 10:00 and immediately switches apps (putting your app in the background). At 10:04 the user returns to your app.

The timer will fire at 10:09, not 10:05.

NSTimer fires based on time the app has spent in the foreground, not the actual specified fire date. This is annoying behavior.

This category fixes the issue by manually fixing the timers when the app re-enters the foreground. Enjoy!

Something went wrong with that request. Please try again.