Type Method

publish(every:tolerance:on:in:options:)

Returns a publisher that repeatedly emits the current date on the given interval.

Declaration

static func publish(every interval: TimeInterval, tolerance: TimeInterval? = nil, on runLoop: RunLoop, in mode: RunLoop.Mode, options: RunLoop.SchedulerOptions? = nil) -> Timer.TimerPublisher

Parameters

interval

The time interval on which to publish events. For example, a value of 0.5 publishes an event approximately every half-second.

tolerance

The allowed timing variance when emitting events. Defaults to nil, which allows any variance.

runLoop

The run loop on which the timer runs.

mode

The run loop mode in which to run the timer.

options

Scheduler options passed to the timer. Defaults to nil.

Return Value

A publisher that repeatedly emits the current date on the given interval.

Discussion

The return type, Timer.TimerPublisher, conforms to ConnectablePublisher, which means you must explicitly connect to the Timer publisher to begin publishing events. You can do this with a call to connect(), or by using autoconnect() to automatically connect when a subscriber attaches, as shown here:

cancellable = Timer.publish(every: 1, on: .main, in: .common)
    .autoconnect()
    .sink() {
        print ("timer fired: \($0)")
}

Topics

Declaring Publisher Topography

typealias Timer.TimerPublisher.Output

The kind of values published by this publisher.

typealias Timer.TimerPublisher.Failure

The kind of errors this publisher might publish.

Creating a Timer Publisher

See Also

Firing Messages as a Combine Publisher

class Timer.TimerPublisher

A publisher that repeatedly emits the current date on a given interval.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software