CFRunLoopTimerSetNextFireDate(_:_:)
Sets the next firing date for a CFRunLoopTimer object .
Declaration
func CFRunLoopTimerSetNextFireDate(_ timer: CFRunLoopTimer!, _ fireDate: CFAbsoluteTime)Parameters
- timer:
The run loop timer to modify.
- fireDate:
The new firing time for
timer.
Discussion
Resetting a timer’s next firing time is a relatively expensive operation and should not be done if it can be avoided; letting timers autorepeat is more efficient. In some cases, however, manually-adjusted, repeating timers are useful. For example, if you have an action that will be performed multiple times in the future, but at irregular time intervals, it would be very expensive to create, add to run loop modes, and then destroy a timer for each firing event. Instead, you can create a repeating timer with an initial firing time in the distant future (or the initial firing time) and a very large repeat interval—on the order of decades or more—and add it to all the necessary run loop modes. Then, when you know when the timer should fire next, you reset the firing time with CFRunLoopTimerSetNextFireDate(_:_:), perhaps from the timer’s own callback function. This technique effectively produces a reusable, asynchronous timer.
See Also
CFRunLoopTimer Miscellaneous Functions
CFRunLoopTimerCreateWithHandler(_:_:_:_:_:_:)CFRunLoopTimerCreate(_:_:_:_:_:_:_:)CFRunLoopTimerDoesRepeat(_:)CFRunLoopTimerGetContext(_:_:)CFRunLoopTimerGetInterval(_:)CFRunLoopTimerGetNextFireDate(_:)CFRunLoopTimerGetOrder(_:)CFRunLoopTimerGetTypeID()CFRunLoopTimerInvalidate(_:)CFRunLoopTimerIsValid(_:)