Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
A Cocoa timer class for iOS and OS X offering a choice of behaviors, so you can use the right one.
Objective-C Ruby
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
Classes
TimerTest
.gitignore
CHANGELOG.md
LICENSE
MyLilTimer.podspec
README.md
Rakefile

README.md

MyLilTimer

Know your clocks

MyLilTimer is a Cocoa timer class for iOS and Mac OS X. Its only dependency is the Foundation framework. MyLilTimer has an interface similar to NSTimer, while also providing a choice of three behaviors (three different clocks).

Read the accompanying article on dev etc: Timers, Clocks, and Cocoa.

Behaviors

The three behaviors are visualized in this YouTube video.

MyLilTimerBehaviorHourglass (MyLilTimerClockBoottime)

The timer fires after an interval has elapsed, regardless of system clock changes and system sleep. This is the behavior people often expect from NSTimer, and it appears to have this behavior when running under Xcode (and when an iOS device is plugged in to power).

MyLilTimerBehaviorPauseOnSystemSleep (MyLilTimerClockMonotonic)

The timer fires after the system has run for some duration; this is paused while the operating system was asleep. This is the actual behavior of NSTimer — an iOS device will sleep when unplugged from power, the screen is locked, and no apps are keeping it awake.

This behavior is appropriate for measuring the speed of long-running operations, such as “3 of 7 items processed, 30 seconds remaining”.

MyLilTimerBehaviorObeySystemClockChanges (MyLilTimerClockRealtime)

The timer fires when the time on the system clock passes the fire date. This behavior is appropriate for an alarm that fires when the clock shows a particular time.

Usage

The most common way to set a timer is with the class convenience method:

+[MyLilTimer scheduledTimerWithBehavior:(MyLilTimerBehavior)behavior
                           timeInterval:(NSTimeInterval)intervalSinceNow
                                 target:(id)target
                               selector:(SEL)action
                               userInfo:(id)userInfo]

The TimerTest iPhone / iPad app demonstrates the use of MyLilTimer, along with all three behaviors.

Requirements

MyLilTimer uses automatic reference counting and the dispatch_once GCD function, which are available on Mac OS X 10.6+ and iOS 4+.

If you so wanted, it would be easy to remove both these dependencies for compatibility to much earlier systems.

Installation

Simply add MyLilTimer.h and MyLilTimer.m to your project file.

Or, with CocoaPods, add to your Podfile:

pod 'MyLilTimer'

(Note: I have never used CocoaPods and don't otherwise endorse it,)

NSTimer features not (yet) supported

  • Repeating timers
  • Changing the fire date
  • Use on run loops other than the main run loop (including background threads)

License

MyLilTimer is released into the public domain, and may be used with or without modification with no restrictions, attribution, or warranty. Attribution is kindly requested, but not required.

Something went wrong with that request. Please try again.