Combine is a new framework, Apple introduced in WWDC 2019. This framework provides a declarative Swift API for processing values over time. Combine allows you to write functional reactive code by providing declarative swift apis. We can compare this framework with RxSwift and RxCocoa.
It allows us to process values over a time. Examples of this kind of value are network response, user interface events and other similar types of asynchronous data.
There are some basic concepts that you need to understand.
- Publisher: Things that produce values
- Operator: Things that do work with values
- Subscriber: Things that care about values
We will explain all 3 points in this blog.
Publisher
Publishers are the basic provider of data when it is available and required. Publisher which has not any request will not provide any data (But obvious).
This protocol has 2 associated types.
- Output: The types of value it produces
- Failure: The types of error it could encounter
Publisher can emit multiple events:
- An Output values of Output type
- Completion of publisher
- A failure with error having Error type
Timer and URLSession is an enhanced type of foundation.
Operator
Operators are special methods that are called on publishers and return the same or different publishers. It is used to change values like adding, removing and many other operations. You can use multiple operators to perform complex processing.
Each operator returns a type that implements the publisher protocol.
Below is example of some operator
Map
Filter
RemoveDuplicates
Subscriber
Subscriber receives values from publisher. Publishers and operators are pointless until someone is listening to the published events.
Subscriber confirms following protocols.
Subscriber can receive a value of type Input or termination event with either success or Failure.
A subscriber receives a stream of value, completions or failure events from a publisher
Let’s combine Subscriber and Publisher.
Publisher will start delivering value when you call subscribe(_:) on it, passing your subscriber. At that point, publishers send subscriptions to subscribers.
Rules of subscription
- A subscriber can only have one condition.
- Zero or more values can be published
- At most one completion will be called
Best example of a complete publisher is URLSessionTask Publisher which will complete tasks either with data response or with error response.
Whenever error will be thrown, subscribers will end even if it has multiple values to pass through..
Below image will help you to understand the flow between publisher and subscriber.
In short, combine is
Combine = Publishers + Subscribers + Operators
Lets connect Publisher and Subscriber
Combine has 2 built in subscribers: Subscribers.Sink and Subscribers.Assign.
You can call them either by calling this method on a publisher.
sink(receiveCompletion:receiveValue:) to handle new element or completion event in closure.
Assign(to:on:) to write new element to a property
- Create a Just publisher that sends a single value and then completes. Combine has number of build in publishers including, Just
- Connect a Subscribers.sink subscriber. It will print below.
Cancellable
First of all in Rx Subscription results in a disposable, which allows you to stop the subscription, Combine does not have this. If you don’t want to receive further updates, you will just de-reference the publisher chain. So apple manages this functionality with Cancellable.
Cancellable protocol has a cancel method which frees up any allocated resources. According to documentation, cancellable is protocol that indicates an activity or actions may be cancelled.
Benefits of using combine framework
- Simplified asynchronous code - No more callbacks.
- Declarative syntax - Easy to read and maintain.
- Multithreading- You don’t have to worry about it.
- Built in memory management - No more bugs to carry.