- iOS 2.0+
- macOS 10.0+
- tvOS 9.0+
- watchOS 2.0+
A CFRunLoopTimer object represents a specialized run loop source that fires at a preset time in the future. Timers can fire either only once or repeatedly at fixed time intervals. Repeating timers can also have their next firing time manually adjusted.
A timer is not a real-time mechanism; it fires only when one of the run loop modes to which the timer has been added is running and able to check if the timer’s firing time has passed. If a timer’s firing time occurs while the run loop is in a mode that is not monitoring the timer or during a long callout, the timer does not fire until the next time the run loop checks the timer. Therefore, the actual time at which the timer fires potentially can be a significant period of time after the scheduled firing time.
A repeating timer reschedules itself based on the scheduled firing time, not the actual firing time. For example, if a timer is scheduled to fire at a particular time and every 5 seconds after that, the scheduled firing time will always fall on the original 5 second time intervals, even if the actual firing time gets delayed. If the firing time is delayed so far that it passes one or more of the scheduled firing times, the timer is fired only once for that time period; the timer is then rescheduled, after firing, for the next scheduled firing time in the future.
Each run loop timer can be registered in only one run loop at a time, although it can be added to multiple run loop modes within that run loop.
CFRunLoopTimer is “toll-free bridged” with its Cocoa Foundation counterpart,
Timer. This means that the Core Foundation type is interchangeable in function or method calls with the bridged Foundation object. Therefore, in a method where you see an
NSTimer * parameter, you can pass in a
CFRun, and in a function where you see a
CFRun parameter, you can pass in an
NSTimer instance. This also applies to concrete subclasses of
NSTimer. See Toll-Free Bridged Types for more information on toll-free bridging.