The core idea of Combine is the Publisher → Subscriber relationship.
A publisher produces values over time, while a subscriber receives and processes those values.
Data flows through a pipeline like this:
Publisher → Operators → Subscriber
In the simplest case (without operators), the flow becomes:
Publisher → Subscriber
The publisher emits values, and the subscriber receives them.
let numbers = [1, 2, 3, 4, 5]
let publisher = numbers.publisher
publisher
.sink(
receiveCompletion: { completion in
print("Completion:", completion)
},
receiveValue: { value in
print("Received:", value)
}
)
.store(in: &cancellables)
A publisher is a source of data that emits values to subscribers.
In this example:
let publisher = numbers.publisher
The array is converted into a publisher.
This publisher emits values sequentially:
1
2
3
4
5
After emitting all values, the publisher sends a completion event.