iOS & Swift

RxSwift: Reactive Programming with Swift

Leverage the power of RxSwift in your reactive apps! In this RxSwift book, you'll learn how to integrate RxSwift into real-world iOS apps. By Florent Pillet, Scott Gardner & Marin Todorov.

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

Who is this for?

This book is for iOS developers who already feel comfortable with iOS and Swift, and want to dive deep into development with RxSwift.

Covered concepts

  • Observables and Observers
  • Filtering, transforming, combining, and time operators
  • RxCocoa
  • Intermediate topics: networking, multi-threading, error handling
  • MVVM app architecture
  • Scene based navigation
  • Exposing data via services

Leverage the power of RxSwift in your reactive apps!

This book is for iOS developers who already feel comfortable with iOS and Swift, and want to dive deep into development with RxSwift.

Start with an introduction to the reactive programming paradigm; learn about observers and observables, filtering and transforming operators,...

more

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: Getting Started with RxSwift

In this part of the book, you’re going to learn about the basics of RxSwift. You are going to have a look at what kinds of asynchronous programming problems RxSwift addresses, and what kind of solutions it offers.

Further, you will learn about the few basic classes that allow you to create and observe event sequences, which are the foundation of the Rx framework.

You are going to start slow by learning about the basics and a little bit of theory. Please don’t skip these chapters! This will allow you to make good progress in the following sections when things get more complex.

1
Toggle description
Learn about the reactive programming paradigm and what RxSwift can bring to your app.
2
Toggle description
Now that you’re ready to use RxSwift and have learned some of the basic concepts, it’s time to play around with observables.
3
Toggle description
In this chapter, you’re going to learn about the different types of subjects in RxSwift, see how to work with each one and why you might choose one over another based on some common use cases.
4
Toggle description
In this chapter, you’ll use RxSwift and your new observable super-powers to create an app that lets users to create nice photo collages — the reactive way.

Section II: Operators & Best Practices

Operators are the building blocks of Rx, which you can use to transform, process, and react to events emitted by observables.

Just as you can combine simple arithmetic operators like +, -, and / to create complex math expressions, you can chain and compose together Rx’s simple operators to express complex app logic.

In this section, you will:

  • Start by looking into filtering operators, which allow you to process some events but ignore others.
  • Move on to transforming operators, which allow you to create and express complex data transformations. You can for example start with a button event, transform that into some kind of input, process that and return some output to show in the app UI.
  • Look into combining operators, which allow for powerful composition of most other operators.
  • Explore operators that allow you to do time based processing: delaying events, grouping events over periods of time, and more. Work though all the chapters, and by the end of this section you’ll be able to write simple RxSwift apps!
Toggle description
This chapter will teach you about RxSwift’s filtering operators that you can use to apply conditional constraints to “.next” events, so that the subscriber only receives the elements it wants to deal with.
Toggle description
In the previous chapter, you began your introduction to the functional aspect of RxSwift. In this chapter, you’re going to try using the filtering operators in a real-life app.
Toggle description
In this chapter, you’re going to learn about one of the most important categories of operators in RxSwift: transforming operators.
In this chapter, you’ll take an existing app and add RxSwift transforming operators as you learn more about map and flatMap, and in which situations you should use them in your code.
Toggle description
This chapter will show you several different ways to assemble sequences, and how to combine the data within each sequence.
Toggle description
You'll get an opportunity to try some of the most powerful RxSwift operators. You'll learn to solve problems similar to those you'll face in your own applications.
Toggle description
Managing the time dimension of your sequences is easy and straightforward. To learn about time-based operators, you'll practice with an animated playground that visually demonstrates how data flows over time.

Section III: iOS Apps with RxCocoa

Since Rx is a multi-platform framework, it doesn’t make any assumptions on which device your Rx powered app is running. RxSwift closely follows the general API design that RxPython, RxRuby, RxJS, and all other platforms conform to, so it does not include any specific features or integrations with UIKit or Cocoa to aid you in developing for iOS or macOS.

RxCocoa is a standalone library (though it’s bundled with RxSwift) that allows you to use many prebuilt features to integrate better with UIKit and Cocoa.

RxCocoa will provide you with out-of-the-box classes to do reactive networking, react to user interactions, bind data models to UI controls, and more.

Toggle description
In this chapter you’ll be introduced to another framework: RxCocoa. RxCocoa works on all platforms and targets the specific UI needs of iOS, watchOS, tvOS and macOS.
Toggle description
Following on from Chapter 12, you’ll learn about some advanced RxCocoa integrations and how to create custom wrappers around existing UIKit components.

Section IV: Intermediate RxSwift/RxCocoa

Once you start writing complete apps with RxSwift and RxCocoa, you will also need to take care of more intermediate topics than simply observing for events and processing them with Rx.

In a full production-quality app, you will need to build an error handling strategy, do more advanced multi-threading processing, create a solid test suite, and more.

In this part of the book, you will work through four challenging chapters, which will lift your Rx status from a rookie level to a battle-tested warrior.

Toggle description
Even the best RxSwift developers can’t avoid encountering errors. You’ll learn how to deal with errors, how to manage error recovery through retries, or just surrender yourself to the universe and letting the errors go.
Toggle description
This chapter will cover the beauty behind schedulers, where you’ll learn why the Rx abstraction is so powerful and why working with asynchronous programming is far less less painful than using locks or queues.
Toggle description
For all the reasons why you started reading this book and are excited to begin using RxSwift in your app projects, RxTest (and RxBlocking) may very soon have you excited to write tests against your RxSwift code, too.
In this chapter, you will create an extension to NSURLSession to manage the communication with an endpoint, as well as managing the cache and other things which are commonly part of a regular application.

Section V: RxSwift Community Cookbook

RxSwift’s popularity keeps growing every day. Thanks to the friendly and creative community that formed around this library, a lot of community-driven Rx projects are being released on GitHub.

The advantage of the community-built libraries that use RxSwift is that unlike the main repository, which needs to follow the Rx standard, these libraries can afford to experiment and explore different approaches, provide non multi-platform specializations, and more.

In this section you are going to look into just a few of the many community open source projects. The section contains five short cookbook-style chapters that look briefly into five community projects that help you with binding table views, handling user gestures, debugging your observables using RxTimelane, and talking to your server with Alamofire.

Toggle description
RxSwift not only comes with the tools to perfectly integrate observable sequences with tables and collections views, but also reduces the amount of boilerplate code by quite a lot.
Toggle description
The RxSwift framework offers a large choice of operators, which can be overwhelming for beginners. But when you start using a lot of Rx functionality in your applications, you may quickly find yourself needing even _more_ operators to solve cases not covered by the core framework. While creating your own operators is easy, you may want to rely on existing, well-tested code that guarantees you’ll always get the results you want. You'll learn about some of them in this chapter.
Toggle description
Action exposes observables for errors, the current execution status, an observable of each work observable, guarantees that no new work starts when the previous has not completed, and generally is such a cool class that you don’t want to miss it!
Toggle description
Gesture processing is a good candidate for reactive extensions. Gestures can be viewed as a stream of events, either discrete or continuous. Working with gestures normally involves using the target-action pattern, where you set some object as the gesture target and create a function to receive updates.
Toggle description
Pro users need powerful debugging tools; in this chapter, you are going to learn the basics of using RxTimelane to inspect and debug your RxSwift code in the Xcode’s Instruments app.
Toggle description
One of the basic needs of modern mobile applications is the ability to query remote resources. RxAlamofire adds an idiomatic Rx layer to Alamofire, making it straightforward to integrate into your observable workflow.

Section VI: Putting It All Together

The “easy” part of the book is over. :] If you made it this far and are looking to learn even more in order to start creating production apps with RxSwift, this section is for you.

The two chapters in this section are going to help you learn how to build real-life applications with RxSwift.

The first chapter will cover the MVVM application architecture and show how a well designed ViewModel can power both the iOS and macOS versions of an app. You will look into building a flexible networking layer and touch on writing tests for your view models.

The second chapter, and the last one in this book, is going to build upon what you learned about the MVVM architecture and expand on it by adding services into the mix and scene-based navigation.

Once you finish working through this section, you will be one of the top RxSwift developers out there. There is, of course, more to know about Rx but at this point you will be able to figure out things further on your own.

Also, don’t forget to give back to the community! It would not have been possible for us to put this book together without all the amazing Rx folks sharing their knowledge, code, and good vibes.

Toggle description
RxSwift is such a big topic that this book hasn’t covered application architecture in any detail yet. And this is mostly because RxSwift doesn't enforce any particular architecture upon your app. However, since RxSwift and MVVM play very nicely together, this chapter is dedicated to the discussion of that specific architecture pattern.
Toggle description
To conclude this book, you’ll architect and code a small RxSwift application. The goal is not to use Rx “at all costs”, but rather to make design decisions that lead toa clean architecture with stable, predictable and modular behavior. The application is simple by design, to clearly present ideas you can use to architect your own applications.