This course is available as part of the raywenderlich.com Professional subscription. To learn more click here
Pro

Intermediate Combine

Apr 13 2021 · Video Course (21 mins) · Intermediate

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.

4.4 / 5 5 Ratings

Version

  • Swift 5.3, macOS 11.1, Xcode 12.2

Intermediate Combine

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

1

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.

2

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.

3

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.

4

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.

5

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.

6

Next course in this Learning 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

Contributors

Comments