Contents

autoconnect()

Automates the process of connecting or disconnecting from this connectable publisher.

Declaration

func autoconnect() -> Publishers.Autoconnect<Self>

Mentioned in

Return Value

A publisher which automatically connects to its upstream connectable publisher.

Discussion

Use autoconnect() to simplify working with ConnectablePublisher instances, such as Timer.TimerPublisher in the Foundation framework.

In the following example, the publish(every:tolerance:on:in:options:) operator creates a Timer.TimerPublisher, which is a ConnectablePublisher. As a result, subscribers don’t receive any values until after a call to connect(). For convenience when working with a single subscriber, the autoconnect() operator performs the connect() call when attached to by the subscriber.

cancellable = Timer.publish(every: 1, on: .main, in: .default)
    .autoconnect()
    .sink { date in
        print ("Date now: \(date)")
    }