Building Reactive Applications With Combine

What Is Combine

Download Your Free Copy of
The Missing Manual
for Swift Development

The Guide I Wish I Had When I Started Out

Join 20,000+ Developers Learning About Swift Development

Download Your Free Copy

Earlier in this series, we defined reactive programming as working with asynchronous streams of data. Now that you know what asynchronous streams of data are, it is clear why asynchronous programming is such a powerful concept.

Asynchronous programming isn't new, though. The Cocoa frameworks have been around for many, many years. They expose a range of asynchronous interfaces you are probably already familiar with. We explored a few examples earlier in this series. The target-action pattern, key-value observing, notifications, and callbacks are examples of asynchronous interfaces the Cocoa frameworks have been using for years and years.

These asynchronous interfaces work fine. They have been serving developers for decades. Why should you consider reactive programming? What does the Combine framework offer that makes these asynchronous interfaces a less compelling option? In other words, what is wrong with the target-action pattern, key-value observing, notifications, and callbacks?

Asynchronous programming is powerful, but it is hard. Reactive programming aims to drastically simplify asynchronous programming. The asynchronous interfaces we have been using for years have their value. The problem is that they have little in common. It is your task as a developer to mix and match these asynchronous interfaces to accomplish your goal. That often results in complexity, such as race conditions and difficult to debug problems.

Let me show you how reactive programming can radically reduce this complexity. How does the Combine framework fit in?

A Unified, Declarative API

Apple describes Combine as a framework that defines a unified, declarative API for processing values over time. Let's break Apple's description up into three components.

Values Over Time

Earlier in this series, we touched on the last piece of this definition, values over time. Reactive programming is working with asynchronous streams of data. This is synonymous to working with values over time. Remember the observer pattern, the core concept that underlies reactive programming. Subjects emit values over time and observers can subscribe to these values over time.

Unified Interface

What makes reactive programming powerful is its unified interface. The Cocoa frameworks expose a range of asynchronous interfaces. You are probably familiar with notifications, the target-action pattern, key-value observing, and callbacks. These are examples of asynchronous interfaces that generate asynchronous streams of data or values over time.

The Combine framework exposes a single, unified interface for working with asynchronous streams of data. This opens up a range of possibilities. The Combine framework enables developers to transform and combine these streams of data. But there is more to like. Combine integrates with system frameworks to facilitate the transition from Cocoa's asynchronous APIs to Combine's unified API.

Declarative API

The API of the Combine framework enables developers to describe their intent. Combine's declarative syntax results in code that is easy to read, understand, and maintain. The framework defines dozens of operators to transform and combine streams of data. These operators can be combined to create the result you need.

What's Next?

You know what reactive programming is and what problem the Combine framework aims to solve. It is time to start using the Combine framework. In the next episodes, you write your first lines of reactive code using the Combine framework.

Download Your Free Copy of
The Missing Manual
for Swift Development

The Guide I Wish I Had When I Started Out

Join 20,000+ Developers Learning About Swift Development

Download Your Free Copy
Next Episode "Getting Your Feet Wet With Combine"