šŸŒ€

Combine

The Combine framework provides a declarative approach for how your app processes events. Rather than potentially implementing multiple delegate callbacks or completion handler closures, you can create a single processing chain for a given event source. Each part of the chain is a Combine operator that performs a distinct action on the elements received from the previous step.

Asynchronous programming

Apple has integrated Combineā€™s API deep into the Foundation framework, so Timer, NotificationCenter and core frameworks like Core Data already speak its language. Luckily, Combine is also very easy to integrate into your own code.

Publishers

Publishers are types that can emit values over time to one or more interested parties, such as subscribers.
1.
An output value of the publisherā€™s genericĀ OutputĀ type.
2.
A successful completion.
3.
A completion with an error of the publisherā€™sĀ FailureĀ type.

Operators

Operators are methods declared on the Publisher protocol that return either the same or a new publisher. Thatā€™s very useful because you can call a bunch of operators one after the other, effectively chaining them together.

Subscribers

Finally, you arrive at the end of the subscription chain: Every subscription ends with a subscriber. Subscribers generally do ā€œsomethingā€ with the emitted output or completion events.
When you add a subscriber at the end of a subscription, it ā€œactivatesā€ the publisher all the way at the beginning of the chain. This is a curious but important detail to remember ā€” publishers do not emit any values if there are no subscribers to potentially receive the output.
Even better, you donā€™t need to specifically memory manage a subscription, thanks to a protocol provided by Combine called Cancellable.
ā€¢
Combine is integrated on the system level. That means Combine itself uses language features that are not publicly available, offering you APIs that you couldnā€™t build yourself.
ā€¢
The ā€œoldā€ style async code via delegates,Ā IBActionĀ or closures pushes you towards writing custom code for each case of a button or a gesture you need to handle. Thatā€™s a lot of custom code to write tests for. Combine abstracts all async operations in your code as ā€œoperatorsā€, which are already well tested.
ā€¢
When all of your asynchronous pieces of work use the same interface ā€”Ā PublisherĀ ā€” composition and reusability become extremely powerful.
ā€¢
Combineā€™s operators are highly composable. If you need to create a new one, that new operator will instantly plug-and-play with the rest of Combine.
ā€¢
Testing asynchronous code is usually more complex than testing synchronous code. With Combine, however, the asynchronous operators are already tested, and all thatā€™s left for you to do is test your business logic ā€” that is, provide some input and test if your subscription outputs the expected result.
Walking through this UML diagram:
1.
The subscriber subscribes to the publisher.
2.
The publisher creates a subscription and gives it to the subscriber.
3.
The subscriber requests values.
4.
The publisher sends values.
5.
The publisher sends a completion.