Intermediate Combine

Combine has a number of operators to help with handling network data, sharing resources with multiple subscribers, and managing errors. Once those are in place, you can perform unit tests on your Combine pipelines to make sure everything is running error-free. By Josh Steele.

Leave a rating/review
Download materials
Save for later
Comments
Share

Learning path

This is part of the Declarative Programming with Combine learning path. View path.

Who is this for?

This course is useful if you already have a basic understanding of Combine, and are interested in learning about more advanced Combine features. If you are familiar with Swift’s URLSession class, error handling, and Xcode testing, it will help you understand the examples.

Covered concepts

  • Combine
  • URLSession
  • Testing
  • Error Handling

Part 1: Intermediate Combine

01
Toggle description

Combine comes with 2 special operators, dataTaskPublisher(for:) and decode() that help you fetch data from the network in your Combine pipeline, and decode that data using Swift’s built in decoders such as JSONDecoder

Toggle description

Publishers in Combine are usually passed by value, since they are structs; however, you can use the built in Combine operator share() to pass them by reference, establishing one true subscription and simply sharing the data with other subscribers as they come along. Want to wait until all of the subscribers are ready before starting the data flow? multicast() allows you to do just that, and start the data when you are ready.

Toggle description

The Subscriber protocols allows developers to define the maximum amount of data that subscriber will accept each time it receives a new value from the Publisher, allowing you to dynamically manage backpressure. However, the adjustment is additive, and must be positive, meaning the max can only go up.

Toggle description

Operators such as tryMap erase any error types that are encountered during execution of the code within the operator. The mapError operator lets developers maintain knowledge of errors encountered, passing them down the Combine pipeline.

Toggle description

Each publisher gets the ability to retry sending their data if errors are encountered - something commonly seen when working with getting data from the network. The retry() operator can be used to retry the publisher a certain number of times before failing, and the catch() operator can be used to fall back to a default set of data if all else fails.

Toggle description

As with all other code, your Combine pipeline code can, and should be, tested. The Given-When-Then pattern is a great way to layout your test code, ensuring that your code works as intended.

Up next

iOS & Swift
Integrate Combine Into an App
One of the best ways to test your Combine skills is to put them to use in an app! In this course, put the ... more

Instructors

Contributors

Josh Steele

Instructor

Adriana Kutenko

Illustrator

Over 300 content creators. Join our team.