Step into Swift Combine
What is Combine?
A unified declarative API for processing values over time.
Often in our code, we have many places where we have some sort of value or event Publisher and some Subscriber is interested in receiving values from that Publisher. And some interested party comes along and establishes a connection between these Publisher and Subscriber. Once the connection is established, the Subscriber sometimes declares that they are interested in receiving values from that Publisher (upstream), after which the Publisher is free to begin sending values to Subscriber (downstream). This goes on until either the Publisher decides to stop sending values, whether because it finished or there was some sort of failure, or by someone choosing to cancel the subscription. This general shape of communication appears throughout our software, whether it’s callbacks or closures or any other situations where there’s asynchronous communication. — Combine in Practice, WWDC 2019
Combine has following three concepts:
Publishers are the declarative part of Combine’s API which describe how values and errors are produced and allow registration of a Subscriber to receive these values over time. They specify two
associatedTypes and a key function as shown below: