Reactive Programming in iOS with Combine

Feb 4 2021 Swift 5.3, macOS 11.0, Xcode 12.2

Part 1: Getting Started

1. Introduction

Lesson Complete

Play Next Lesson
Next
Save for later
About this episode

See course reviews

See forum comments
Cinema mode Mark as Complete Download course materials
Next episode: 2. Publishers and Subscribers

Prerequisites: Xcode 12.2 & macOS Big Sur

Hey everyone, I’m Josh, and welcome to Reactive Programming in iOS with Combine! That course title alone may be seem a little daunting at first, especially if you haven’t done reactive programming before.

And perhaps you initially took a look at the documentation for Combine, and saw this. Although it’s accurate, you might find this definition to be a bit… elusive and abstract. For example, what is a “single processing chain for a given event source”?

That’s why, before delving into code in the following chapters, you’ll take some time to learn about the problems Combine solves, and the tools it uses to do so.

Armed with this knowledge, you’ll then be ready to tackle the rest of the course, and by the end of it, you’ll be programming reactively and Combine-ing all the things.

Asynchronous programming

Let’s see the primary thing Combine seeks to improve for you.

In a simple, single-threaded language, a program executes sequentially line-by-line, something like this. Synchronous code is easy to grasp because it’s easy to understand the state of your data every step of the way. This code will always print “Ron” and then “Ron Burgundy.”

Now, imagine you wrote a program in a multi-threaded language that is running an asynchronous event-driven UI framework.

Here, the code sets name’s value to "Ron" and then appends "Burgundy" to it on thread 1. But at some non-deterministic point, name is set to "Brick Tamland" on a second thread.

When the code is running concurrently on different cores, it’s difficult to say which part of the code is going to modify the shared state first.

What exactly happens here depends on the system load, and you might see different results each time you run the program.

Managing mutable state in your app is very complicated once you run asynchronous concurrent code. And it’s a whole lot easier to miss edge cases and leave them unaddressed.

Apple has been improving asynchronous programming for their platforms over the years, and they’ve created several ways to write and execute asynchronous code. You’ve probably used some if not all of these in your projects without giving them a second thought, because they’re so fundamental to writing apps.

Writing good asynchronous programs using these APIs is possible. It’s just more complex than… well, we’d like it to be.

Asynchronous code and resource sharing is notoriously difficult to troubleshoot. The word “chaos” comes to mind. Combine introduces a single, unified approach to asynchronous programming to the Swift ecosystem. It helps you bring order to the chaos and write more succinct code that is easier to read and troubleshoot. And Apple has designed their new UI framework SwiftUI to work hand-in-glove with Combine.

In fact, Apple has baked Combine into several frameworks across their SDKs, as shown in this simple diagram. Clearly Apple is committed to streaming asynchronous programming with Combine.

Combine doesn’t replace these frameworks, it integrates with them and allows all the types in your app that want to talk asynchronously to each other do so via a new, streamlined approach.

So if the idea of using the same asynchronous tools to connect all the parts of your app — from the data model to the networking layer and the user interface — sounds interesting, great! You’re in the right place.

Before learning Combine, it is helpful to understand how it came to be.

Reactive programming isn’t a new concept. It’s been around for quite a while, but it’s made a fairly noticeable comeback in the last decade.

The first “modern-day” reactive solution came in 2009 when a team at Microsoft launched a library called Reactive Extensions for .NET, called Rx.NET.

Microsoft opensourced the Rx implementation in 2012, and as a result, several different languages started to use its concepts.

Currently, there are many ports of the Rx standard like RxJS, RxKotlin, RxScala, RxPHP and for Apple platforms…

RxSwift. And inspired by Rx, additional reactive frameworks have been created for Swift, including ReactiveSwift, and now…

Combine. Combine implements a standard that is different but similar to Rx, called Reactive Streams. It has a few key differences from Rx, but they both agree on most of the core concepts.

One thing to note, though. Combine is only available for use in apps that target iOS 13 or macOS Catalina or later, as well as tvOS 13 and watchOS 6, or later.

Adoption of these latest versions will no doubt surge, as will demand for the Combine skills you’ll learn in this course.

One last thing before you get started. Let’s make sure you have everything you need to go through this course.

You should be pretty comfortable with Swift and have at least a basic familiarity with iOS. If not, check out our iOS and Swift for Beginners learning path on raywenderlich.com.

You’ll need Xcode 12 or higher, which can be downloaded from the App Store.

macOS Catalina or higher is needed to run that version of Xcode, which can also be downloaded from the App Store.

Xcode 11 (or above) has a pretty cool new feature called Live Previews. You don’t need it to go through this course, but if you do want to check it out, you’ll need macOS Catalina or higher. Note that at the time this course was recorded, macOS Big Sur and Xcode 12.2 were used.

This course is organized into 4 parts, covering the main components of Combine.

You’re in the first part now: Getting Started. Next you’ll learn about publishers and subscribers, creating subscriptions, and more.

In parts two through four, you’ll learn all about Combine operators, which is just a fancy name for methods that let you manipulate data and events coming from publishers.

By the end of this course, you will have a very full toolbox of Combine operators, and when “combined” with the various Publishers and Subscribers iOS provides, will prepare you for the new world of reactive programming

Let’s recap. In this episode, you’ve: gained an initial understanding of what Combine is and how it can help make your code safer and more reliable; run through the checklist of what you’ll need to be successful with this course; you’ve gotten a small sample of what’s in store for you in the upcoming episodes.

Hopefully you’re excited and ready for more — here we go!