Logo for tanaschita.com

How to use a Connectable publisher in Combine

Learn how to prevent data loss by using Combine's ConnectablePublisher.

18 Nov 2024 · 3 min read

When working with publishers in Combine, there are situations where we need precise control over when values start emitting. By default, Combine publishers begin emitting as soon as they are created, which can lead to subscribers missing some values if they aren't connected in time.

This is where Combine’s ConnectablePublisher comes in. A ConnectablePublisher holds off emitting values until its connect() method is called, giving us more control over when subscribers start receiving values.

Sponsorship logo
Architecture & Design Patterns for iOS
This book is a practical guide to essential architectural principles and design patterns for iOS development. It covers strategies for building maintainable apps with Swift and SwiftUI, including dependency injection, navigation, common patterns, and modularization.
LEARN MORE

Let's look at an example.

let publisher = Just(5)
subscriber1 = publisher.sink { newValue in
print(newValue)
}
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
self.subscriber2 = publisher.sink { newValue in
print(newValue)
}
}

Here, subscriber2 misses the emitted value 5 because the Just publisher emits it immediately, and subscriber2 subscribes too late.

Solving the issue with ConnectablePublisher

To ensure both subscribers receive the value, we can convert the publisher to a ConnectablePublisher by calling makeConnectable() and controlling when it starts emitting values using the connect() method:

let publisher = Just(5).makeConnectable()
subscriber1 = publisher.sink { newValue in
print(newValue)
}
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
self.subscriber2 = publisher.sink { newValue in
print(newValue)
}
self.connection = publisher.connect()
}

Now, the publisher will only emit the value after connect() is called, ensuring both subscribers receive the value.

How to automatically connect with first subscriber

If we want a ConnectablePublisher to start emitting values right after the first subscriber subscribes, it provides the autoconnect() method.

self.timerSubscriber = Timer.publish(every: 5, on: .main, in: .default)
.autoconnect()
.sink() { date in
print (date)
}

In the example above, the publish method of Timer returns a ConnectablePublisher. By calling autoconnect() the timer starts sending date values as soon as we subscribe.

Sponsorship logo
Preparing for a technical iOS job interview
Preparing for a technical iOS Job Interview with over 300 questions & answers. Covering Swift & Objective-C, SwiftUI & UIKit, Combine, HTTP Networking, iOS File System, Core Data, Concurrency with async/await, Security, Automated Testing, Dependency Management, AI & Machine Learning and more.
LEARN MORE
Sponsorship logo
Architecture & Design Patterns for iOS
This book is a practical guide to essential architectural principles and design patterns for iOS development. It covers strategies for building maintainable apps with Swift and SwiftUI, including dependency injection, navigation, common patterns, and modularization.
LEARN MORE
Sponsorship logo
Become a sponsor of tanaschita.com
By publishing an article on different iOS topics every week, tanaschita.com is constantly growing in the developer community and may provide a great audience for you as a sponsor.
CLICK TO LEARN MORE

Newsletter

Image of a reading marmot
Subscribe

Like to support my work?

Say hi

Related tags

Articles with related topics

combine

swiftui

swift

ios

How to delay server requests for user's search query with SwiftUI and Combine

Learn how to debounce on a practical example.

15 Jul 2024 · 5 min read

Latest articles and tips

© 2026 tanaschita.com

Privacy policy

Impressum