iOS & Swift

Combine: Asynchronous Programming with Swift

The best book to master declarative asynchronous programming with Swift using the Combine framework! By Shai Mishali, Florent Pillet, Marin Todorov & Scott Gardner.

Read for Free with the Personal Plan* * Includes this and all other books in our online library See all benefits
Buy Individually $59.99* *Includes access to all of our online reading features.
Leave a rating/review
Download materials
Save for later

Who is this for?

Combine uses a multitude of advanced Swift features such as generics, so you should have at least an intermediate-level knowledge of Swift.

Covered concepts

  • Reactive programming
  • Combine operators
  • Usage in real-world apps
  • SwiftUI with Combine
  • Error handling
  • Custom publishers
Master declarative asynchronous programming with Swift using the Combine framework!

Writing asynchronous code can be challenging, with a variety of possible interfaces to represent, perform, and consume asynchronous work — delegates, notification center, KVO, closures, etc. Juggling all of these different mechanisms can be somewhat overwhelming. Does it really have...


Before You Begin

This section tells you a few things you need to know before you get started, such as what you’ll need for hardware and software, where to find the project files for this book, and more.

Section I: Introduction to Combine

In this part of the book, you’re going to ramp up over the basics of Combine and learn about some of the building blocks it comprises. You’ll learn what Combine aims to solve and what are some of the abstractions it provides to help you solve them: Publisher, Subscriber, Subscription, Subject and much more.

Toggle description
A gentle introduction to what kind of problems Combine solves, a back story of the roots of reactive programming on Apple's platforms, and a crash course into the basic moving pieces of the framework.
Toggle description
The essence of Combine is that publishers send values to subscribers. In this chapter you’ll learn all about what that means and how to work with publishers and subscribers, and manage the subscriptions that are created between the two of them. This will start you on your foundation for learning Operators, making an app that creates simple photo collages, testing and debugging.

Section II: Operators

If you think of Combine as a language, such as the English language, operators are its words. The more operators you know, the better you can articulate your intention and the logic of your app. Operators are a huge part of the Combine ecosystem which lets you manipulate values emitted from upstream publishers in meaningful and logical ways.

In this section, you’ll learn the majority of operators Combine has to offer, divided into useful groups: transforming, filtering, combining, time manipulation and sequence. Once you’ve got your operator chops down, you’ll wrap up this section with a hands-on project to practice your newly-acquired knowledge.

Toggle description
Before a subscriber receives values from a publisher, you’ll often want to manipulate those values in some way. One of the most common things you’ll want to do is transform those values into some form that is ideal for use by the subscriber. By the end of this chapter you’ll be transforming all the things.
Toggle description
In this chapter you'll learn about filtering elements from Combine publishers, so you can easily control the values published by the upstream and only deal with the ones you care about.
Toggle description
Publishers are extremely powerful, but they're even more powerful when composed together! This chapter will teach you about Combine's combining operators which let you take multiple streams and create meaningful logical relationships between them.
Toggle description
A large part of asynchronous programming relating to processing values over time, this chapter goes into the details of performing complex time-based tasks that would be hard to do without Combine.
Toggle description
When you think about it, Publishers are merely sequences. As such, there are many useful operators that let you target specific elements, or gather information about the sequence as a whole, which you'll learn about in this chapter.
Toggle description
It's time to try your new Combine skills in a real project and learn how to make Foundation and UIKit play along with your reactive code.

Section III: Combine in Action

You now know most of Combine’s basics. You learned how publishers, subscribers and subscriptions work and the intertwined relationship between these pieces, as well as how you can use operators to manipulate publishers and their emissions.

While theory is great and definitely useful, practical real-life knowledge is king!

This section is divided into five mini-chapters, each with its own focus on practical approaches for leveraging Combine for specific use-cases. You’ll learn how to leverage Combine for networking, how to debug your combine publishers, how to use timers and observe KVO-compliant objects, as well as learn how resources work in Combine.

To wrap up this section, you’ll get your hands dirty and build an entire Combine-backed network layer — how exciting!

Toggle description
This chapter teaches you how to seamlessly integrate network requests in Combine publisher chains and make your networking easier and more resilient.
Toggle description
Debugging asynchronous code being notoriously harder linear code, Combine helps with a few tools that will greatly help you improve your understanding of the processes at play.
Toggle description
A niche part of your code, timers are always useful to have around and easy to create in Combine, as you’ll learn in this chapter.
Toggle description
This chapter shows you how Key-Value Observing, which was historically the best way to get notified when a value changed, now integrates nicely with Combine publishers.
Toggle description
Precisely controlling resource usage is a touchy subject, but this chapter will shed light on sharing operators which let you do just that.
Toggle description
In this chapter you will work on a networking API that talks in real-time to the Hacker News servers and along the way exercise using multiple operators and publishers in Foundation.

Section IV: Advanced Combine

With a huge portion of Combine foundations already in your tool belt, it’s time to learn some of the more advanced concepts and topics Combine has to offer on your way to true mastery.

You’ll start by learning how to use SwiftUI with Combine to build truly reactive and fluent UI experiences and switch to learn how to properly handle errors in your Combine apps. You’ll then learn about schedulers, the core concept behind scheduling work in different execution contexts and follow up with how you can create your own custom publishers and handling the demand of subscribers by understanding backpressure.

Finally, having a slick code base is great, but it doesn’t help much if it’s not well tested, so you’ll wrap up this section by learning how to properly test your new Combine code.

Toggle description
SwiftUI is the new user interface paradigm from Apple and it's designed for an interplay with Combine on a whole new level. In this chapter you are going to work through a Combine based SwiftUI project.
Toggle description
This chapter will teach about Combine's powerful typed error system, and how you can leverage it to handle errors in your app, and within Combine publishers.
Toggle description
In this chapter you’ll learn what Schedulers are, how they relate to RunLoops, Dispatch queues and Threads and how Combine extends Foundation to integrate with them.
Creating your own publishers and operators is an advanced topic you’ll learn to master in this chapter, while also learning about and experimenting with backpressure management.
Toggle description
This chapter will introduce you to unit testing techniques for use with Combine code. You’ll go from testing basics in a playground to applying what you’ve learned in adding tests to an existing iOS app project.

Section V: Building a Complete App

Mastery takes practice, and practice you shall!

You’ve made it through this entire book, an amazing feat by all means. It’s time to truly solidify the knowledge you’ve acquired throughout this chapter and build an entire app using Combine and SwiftUI.

You’ve gained valuable skills throughout this book, and in the last section you picked up some advanced Combine know how too. In this final chapter, the pièce de résistance, you’ll build a complete app that applies what you’ve learned—but the learning is not done yet! Core Data in Combine anyone?