Understanding publishers and subscribers
You may recall seeing the terms publish
and subscribe
in the previous recipes. In Combine, these are the core concepts working behind the scenes to create reactive data streams.
When you think about it, it reveals a lot about how it all works. A publisher broadcasts information, and subscribers choose to tune in and listen for relevant data to react to.
We’ll explore this idea in this recipe where we’ll publish the results of a network call for Puppy
information, and then subscribe to that publisher with some instructions on what to do.
How to do it...
We will start with a new playground:
- Create a new playground in Xcode. Go to File | New | Playground | iOS.
- For this, we will need to import both Foundation and Combine at the top.
- First, let’s make our
Puppy
struct. We expect to read three properties from JSON on our network call:struct Puppy: Decodable {
let...