Reactive Programming in iOS with Combine

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

Part 3: Combining Operators

15. Prepend

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: 14. Introduction Next episode: 16. Append

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.

With transforming and filtering operators in your tool belt, it's time to move on to a complex, but useful category of operators, combining operators. These operators let you combine events emitted by different publishers to create combinations of data to send to consumers. A great example of this is performing authentication in your app. You'll need to get a username, password, and checkbox value to remember the username, from the user, combine them, and send them down stream. Let's start with the family of prepending operators. We'll go over each type, and then demo them in a playground. The first prepend operator we'll look at is a version that takes a variatic list of values denoted by the variatic syntax, which is three periods in a row. This means it can take any number of values, as long as they are the same type as the original publisher. Look at the marble diagram. The original publisher only emits three and four, with the prepend operator, you can prepend the values one and two, sending one, two, three, and four to the consumer. A variation of prepend takes in a sequence, conforming object instead of a variatic set of values. This marble diagram shows the example passing in an array of values instead of the variatic collection, but the result is the same- one, two, thee, and four get sent to the consumer. Keep in mind that not all sequence-conforming objects are equal with respect to ordering. Set are un-ordered while arrays are ordered. Another variation of prepend prepends the values emitted by a second publisher before the values of the original publisher. Here, the marble diagram shows the original publisher emitting three and four, and a second publisher emitting one and two. As before, the final sequence is one, two, three, and four, and let's look at all of these in a demo. Make an instance of the example of method to examine the behavior of the prepend output operator. Make an initial publisher from an array that contains three and four. Connect the sync subscriber to the publisher and also call the store method to store the subscriptions in a collection for bulk cancellation when the collection is de-initialized. Add a prepend operator between the publisher and sync subscriber, and use the values one and two as the arguments. Executing the example code shows that the values get prepended and one, two, three, and four are emitted. Also, since operators can chain, add another operator to prepend minus one and zero in front of that, and re-run the playground. Make another example of stub to explore prepend sequence, with five, six, and seven in the initial array. This time, instead of a variatic list, add a prepend operator with an array containing three and four, and following that, add a set containing a range from one to two. Executing this example causes the sync to receive and print out one through seven. Finally, add a prepend with a stride that generates every other value from six through eleven, since stride-able conforms to sequence. This will prepend six, eight, and ten to the string generated by the sync subscriber. The prepend publisher operator is fairly straightforward. Make another example of stub, this time with two publishers, one with an array with three and four in it, and another with one and two in it. Insert the prepend operator between publisher one and the sync. Publisher two will emit values before publisher one emits it's values, again resulting in the one, two, three, and four sequence. Remember though, that for this sort of operator to work, the publisher has to send a completion event so the original publisher's values can be emitted. Change the above example so that publisher two is a pass-through subject publisher, allowing you to send one value at a time. Send values one and two through publisher two and run the playground. You'll see that nothing else ever gets emitted. This is because there has been no indiction that publisher two is done. To fix this, send a completion event through publisher two and re-run the playground. Now you see that one, two, three, and four all make their way to the sync for output to the console. In this episode, you learned prepending operators, which add emissions from one publisher before the original publisher. The prepend output operator take a variatic list of values as long as they are the same type as the original publisher. The prepend sequence operator takes any collection that conforms to the sequence protocol. Don't forget that some sequences are ordered and some aren't. Finally, the prepend publisher operator emits the values from the pass to end publisher before emitting the values from the original publisher. The pass to end publisher must sent a completion event, so the operator knows when to emit values from the original publisher. In the next episode, we'll look at a close relative of the prepend family of operators, the append family.