Publishers & Subscribers
Understand the core Combine flow.
Publishers & Subscribers is a free SwiftUI Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the SwiftUI Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Meet Combine
Combine is Apple's framework for handling values that arrive over time, like taps, timers, or network responses. 🌊
Values Over Time
Instead of asking for data once, you describe a stream of values and react each time a new one arrives, now or later.
What a Publisher Does
A Publisher is the source. It emits values downstream and tells you when it finishes or fails.
let publisher = [1, 2, 3].publisherOutput and Failure
Every publisher declares two types: its Output (the values it sends) and its Failure (the error it may throw).
Just(42) // Output: Int, Failure: NeverWhat a Subscriber Does
A Subscriber attaches to a publisher, receives each value as it arrives, and decides what to do with it.
Connecting with sink
The sink subscriber is the simplest one: you hand it a closure that runs for every value received.
publisher.sink { value in
print(value)
}Handling Completion
A fuller sink takes two closures: one for completion (finished or failure) and one for each value.
publisher.sink(
receiveCompletion: { print($0) },
receiveValue: { print($0) }
)Storing the Subscription
Subscribing returns a cancellable. Keep it alive, or the stream is torn down immediately.
var bag = Set<AnyCancellable>()
publisher.sink { _ in }
.store(in: &bag)Just and Empty
Just emits one value then finishes. Empty finishes with no values at all. Both are handy building blocks.
let one = Just("hello")Subjects Push Values
A PassthroughSubject lets you send values manually, perfect for forwarding events you control.
let subject = PassthroughSubject<Int, Never>()
subject.send(5)The Pull and Push Flow
The subscriber signals demand, the publisher pushes values, and completion ends the relationship cleanly.
Quick Check
You attached a closure to a publisher and nothing printed. What likely went wrong?
Recap: The Combine Trio
You met the three players: a publisher emits values, a subscriber receives them, and a stored cancellable keeps the link alive. 🎉
Frequently asked questions
Is the “Publishers & Subscribers” lesson free?
Yes — the full text of “Publishers & Subscribers” is free to read here on the web, and the SwiftUI Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the SwiftUI Academy course, upgrade to CoddyKit PRO.
What will I learn in “Publishers & Subscribers”?
Understand the core Combine flow. You practise SwiftUI Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start SwiftUI Academy?
No prior experience is required. SwiftUI Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Publishers & Subscribers” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this SwiftUI Academy lesson?
Yes. Every SwiftUI Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Publishers & Subscribers
- Transforming with map & filter
- Debounce for Live Search
- Bridging Combine into SwiftUI