A subscriber is the component in Combine that receives values from a publisher.
While a publisher produces data, the subscriber consumes that data and performs some action with it.
Conceptually:
Publisher → Subscriber
The subscriber listens for:
values
completion events
Subscribers are typically attached to publishers using operators like sink.
let publisher = ["A", "B", "C"].publisher
publisher
.sink(
receiveCompletion: { completion in
print("Subscriber received completion:", completion)
},
receiveValue: { value in
print("Subscriber received value:", value)
}
)
.store(in: &cancellables)
This pipeline connects a publisher to a subscriber.
sink Subscribersink is the most common built-in subscriber in Combine.
It allows you to provide closures that handle:
received values
completion events
Syntax:
sink(receiveCompletion:receiveValue:)
Example: