← All articles
2 min readManav

Reactive Programming in Swift using Combine Framework

In WWDC 2019, Apple released a new framework for processing asynchronous data called Combine. This is the newest declarative API for processing data over…

swiftiosreactive-programmingfunctional-programming

In WWDC 2019, Apple released a new framework for processing asynchronous data called Combine. This is the newest declarative API for processing data over time.

In this blog post, we will navigate through key concepts of Combine Framework.

#Introduction

According to Apple,

The Combine framework provides a declarative Swift API for processing values over time. These values can represent many kinds of asynchronous events.

This is similar to reactive frameworks like RxSwift. This provides a unified, declarative API for processing values over time.

Before diving into code, let’s see some of the core concepts of Combine.

#Key Concepts

There are primarily four key concepts in Combine :

Publisher: Publisher is the protocols which define how data and error are produced. They provide a continuous sequence of data over time. These are value types. It is described with two associated types: one for Output and one for Failure. This has a subscribe(:) method which connects a subscriber to the publisher. Combine provides a number of convenience publishers like Just ,Future ,Publisher.Sequence .

Let’s see an example for subscriber :

https://medium.com/media/4a95fbf4cdf6bacd34035f9c6e396cc1/href

Subscriber: Subscriber is the protocol used to receive data from a publisher after subscribing to a publisher. These are reference type. It is described with two associated types: one for Input and one for Failure. This has three main methods :

  • receive(subscription:) Called when the subscriber has successfully subscribed to the publisher and may request items.
  • receive(_:) Called when a new data is published by the publisher.
  • receive(completion:) Called when there are a finite sequence of data and publisher has complete publishing.

Let’s see an example for subscriber and publisher :

https://medium.com/media/335deae1a56e32dd8c3d9188f5562c23/href

Subjects: Subjects are a special case of Publisher that also conform to the Subject protocol. They are basically publishers on which we can subscribe but also dynamically send events to them. This protocol requires subjects to have a .send() method to allow the developer to send specific values to a subscriber. There are two types of built-in subjects with Combine: CurrentValueSubject and PassthroughSubject. These act similarly, the primary difference being a PassthroughSubject doesn’t have an initial value or a buffer of the most recently-published element.

https://medium.com/media/8b778d829ba0d2047e500faedf7730e6/href

Operator: Operators are classes that adopt Publisher protocol. They support subscribing to a publisher and sending results to any subscribers. This describes the behavior for changing value. This can be used to transform both types — the Output and Failure type. These are value types. There are three groups of operators on Combine — Transforming, Filtering, and Combining Operators. Some of the operators are map ,flatmap ,filter,merge.

https://medium.com/media/04db426839729c6aedab07802e029ccd/href

We can also combine two or more publishers using zip, merge and CombineLatest :

https://medium.com/media/5b4446e94ae13a43763452d8bf025b84/href

#Conclusion

Hopefully, this blog post would help you get started with Combine. However, many of the above topics can be a separate topic on its own. In the next blog, we would explore integrating Combine in other frameworks.


Reactive Programming in Swift using Combine Framework was originally published in Xebia Engineering Blog on Medium, where people are continuing the conversation by highlighting and responding to this story.

Found this useful? Share it.

Keep reading