Reactive Programming in iOS with Combine

Feb 4 2021 · Swift 5.3, macOS 11.0, Xcode 12.2

Part 1: Getting Started

01. Introduction

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Next episode: 02. Publishers and Subscribers

Notes: 01. Introduction

Prerequisites: Xcode 12.2 & macOS Big Sur

Transcript: 01. Introduction Hey everyone, I'm Josh and welcome to Reactive Programming in iOS with Combine. That course title alone may 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 combining all the things. 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 names value to "Ron" and then appends "Burgundy" to it on thread one. 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. An Apple has designed their new UI framework, Swift UI, 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 of Microsoft launched a library called Reactive Extensions for.NET called Rx.NET Microsoft open source 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 Rx.JS, 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 know though, Combined nine 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 search as will demand for the Combined 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 Raywinnerlike.com. You'll need X code 12 or higher, which can be downloaded from the app store MacOS Catalina or higher is needed to run that version of X code, 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 X code 12.2 were used. This course is organized into four 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 combined 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'll 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. Okay, let's recap. In this episode you've gained an initial understanding of what Combined 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, and 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.