Reactive Programming in iOS with Combine

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

Part 3: Combining Operators

19. Operator Examples

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: 18. More Combining Operators Next episode: 20. Challenge: Advanced Combining

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.

Let's take a look at the switchToLatest operator. First, define three PassthroughSubject publishers inside an example of stub. These will be the publishers that our main publisher called publishers will emit. Know the publishers value type is PassthroughSubject and Never, which means it will emit publishers as its value type. Then build the usual publisher operator sync store chain, as you have done throughout the examples in this course. The operator in this case is switchToLatest. If we run the playground now, nothing happens. We haven't sent any publishers into our publisher. Send publisher1 to publishers and then send values one and two to publisher1. Now switch publishers, send publisher2 to publishers. And then send three more values, the value three on publisher1 and the values four and five on publisher2. When we switched to publisher2, the active publisher, publisher1, has its subscription canceled. What do you think will happen when we try to send the value three to publisher1? Finally, send publisher3 to publishers, send another value on publisher2 and the rest of our sequence on publisher3. Again, after the switch, publisher2 has its subscription canceled. Don't forget to send completion events to the last publishers to be active, publisher3 and publishers. Run the playground. You'll see that the sequence of values written to console is one, two, four, five, seven, eight, nine. As expected any value sent to the old publisher after a switch, don't get emitted to the downstream consumer. Now let's take a look at the merge operator. Again, make an example of stub and create two publishers, both PassthroughSubjects, the taken integer value types. Set up the usual publisher operators sync set of code, with publisher1 as the main publisher and the merge operator taking in publisher2 as its argument. Since these are PassthroughSubject publishers, we send the values in, one by one. Bounce back and forth, sending values to publisher1 and publisher2, and send completion events to both publishers when you're done. Run the playground. The sequence printed to the console is in the same order we entered it. One, two, three, four, five, even though we did so with two different publishers. Let's look now at combineLatest. Remember that combineLatest can combine publishers with different value types. And an example of stub defined two publishers. One that emits Ints and another that emits Strings and build our example pipeline using publisher1 as the source publisher and use combineLatest with publisher2 as its argument. Again, bounce back and forth, sending integers to publisher1 and strings to publisher2, as well as completion events for both when you're done. Run the example. Recall that combineLatest only emits tuples of the latest values from each publisher, when each publisher has emitted at least one value. So, the first tuple emitted is 2a, followed by 2b and then 3b and finally 3c. Finally, let's look at zip, which is like combineLatest, but combines values based on their index in each publisher. Again, set up two publishers in the example pipeline like you did with combineLatest, but use the zip operator instead. Now send values to publisher1 and publisher2 in any order you want, but again sending completion events when you're done. Run the playground. You'll see that the tuples are emitted in order. For example, the first tuple to be emitted, is composed of the first values from each publisher 1a, even though other values have been sent to publisher1 before a is sent to publisher2. Each subsequent pairing, if they exist before completion events are sent, get emitted for downstream consumption. So, 2b and 3c also get emitted here. In this episode, you learned about some more advanced combining operators. While switchToLatest is relatively complex, it's extremely useful. It takes a publisher that emits publishers, switches to the latest publisher and cancels the subscription to the previous publisher. Merge with lets you interleave values from multiple publishers. CombineLatest emits the latest values of all combined publishers whenever any of them emit a value. Once all of the combined publishers have emitted at least one value. Zip pairs emissions from different publishers, emitting a tuple of pairs after all publishers have emitted a value. And you can mix combination operators to create interesting and complex relationships between publishers and their emissions. I think it's about time to combine all of your knowledge about combination operators and do another challenge. When you're ready, head on over to the next episode, to put your skills to the test.