Skip to content
forked from jmah/MyLilTimer

A Cocoa timer class for iOS and OS X offering a choice of behaviors, so you can use the right one.

License

Notifications You must be signed in to change notification settings

TribeHR/MyLilTimer

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

A Cocoa timer class for iOS and OS X offering a choice of behaviors, so you can use the right one.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Objective-C 84.1%
  • Ruby 15.9%