Reactive Programming in iOS with Combine

Feb 4 2021 · Swift 5.3, macOS 11.0, Xcode 12.2

Part 2: Transforming & Filtering Operators

08. More Transforming Operators

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 07. Transforming Operators Next episode: 09. Challenge: Create a Phone Number Lookup

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

We covered some of the basic transforming operators, so let's take a look at a few more that are a tad more complex. Sometimes you want to build upon the previous results of an operator, and that's where the scan operator comes into play. This operator takes the next value from the publisher as well as the most recent value returned from the closure. As you can see in the marble diagram, when the first value comes in, there's no recent value from the closure, so the first value is emitted. When the next value comes in, two, that value, as well as the most recent value from the closure, one, comes in for an emitted value of three. Let's look at this in an example. Add this code to your playground. Here we have a computed property that returns a random number between minus 10 and 10. Then we use that to create a publisher from an array of random managers representing a stock price. Finally, the scan operator with a starting value of 50 is used on the publisher to simulate a daily change to the stock price. The use of max here keeps the stock price positive. Running the playground and expanding the result inline in the playground shows the stock behavior. One more transforming operator is flatMap. Sometimes you want to take the output from more than one upstream publisher and combine them for use in a subscriber downstream. That's where flatMap comes into play. FlatMap does not have to, and often will not have to be of the same type as the upstream publisher it receives. A common case for flatMap and combine is to subscribe to properties of values emitted by a publisher that are themselves publishers. Let's look at an example, and afterwards you'll look at the marble diagram. If you take a look at the playground support file, you'll see the definition of the chatter struct. It has two properties, one of which is the name, a string, and the other is a message, which is a current value subject, a publisher. The past and message string to the chatter struct is used to initialize the publisher. Going back to the main playground, add this code. Here you made two chatter instances, one for Charlotte, the other for James. Then you created a chat publisher initialized with the Charlotte publisher. Finally, you've attached a sync subscriber to the chat publisher to output the message. Run the playground and see the message for Charlotte is printed to the console. Now add the following to the playground. This time you'll see Charlotte's and James's introductory message, but not the new value for Charlotte's message. This is because we're subscribed to the chat publisher, which is a chatter publisher. We're not subscribed to the message publisher of each chatter struct. Here's where flatMap comes into play. Replace the chat sync store block of the code with the following. Here we're flatMapping into the chatter structure's message property, and updating the handler to print out the value, which is now a string and not a chatter instance. Run the playground now, and you'll see Charlotte's new message printed. Let's look at one more example. Add the following to the playground and then run it, but before you do, pause, and think about what will be written to the console. You see both of the new messages even though the current value of the chat publisher is James. This is the power of flatMap. Both publishers have been combined into one output. Here's an example of a flatMap diagram showing a special case of flatMap, limiting the number of publishers that the operator will accept. Three publishers come in, but the operator deals only with two. So only the publishers inside publishers one and two are processed, combined, and sent downstream. Okay, we've gone a little deeper into transforming operators in this episode, taking a look at the somewhat more complex scan and flatMap operators. Scan lets you build upon the most recent output from the operator, and flatMap lets you combine values from multiple publishers into a single publisher to send to a downstream consumer. This is commonly seen with publishers that themselves emit publishers, but not always. Okay, we're done with transferring operators, so who's up for a challenge? In the next episode, we'll put your skills to the test.