Reactive Programming in iOS with Combine

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

Part 3: Combining Operators

16. Append

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: 15. Prepend Next episode: 17. Challenge: Append and Prepend

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.

When you were learning about prepending operators in the last episode, were you thinking, "there's got to be appending operators too, right?". Well, you are correct. Let's look at the family of appending operators. We'll go over each type and then demo them in a Playground. The appending family of operators work in an opposite manner to the prepend operators. Adding values after instead of before the original publisher. The variadic form of append takes in a list of values to append to the upstream publisher's set of values. The sequence version of the append operators takes a sequence conforming collection of values to append to the upstream publisher's set of values. Don't forget, not all sequenced conforming collections deal with sorting in the same way. Sets and arrays are sorted differently. And the publisher version of the append operator appends the entire set of values from the passed in publisher onto the end of the original set of values, sending the combined contents of the original publisher, followed by the passed in publisher downstream to the consumer. Let's look at these in a Playground. As discussed, append works opposite of prepend. So given a publisher, we can call append, even multiple times thanks to operator chaining, to append additional values onto the end of the original publisher. Set up the example of stub and have the original publisher come from an array with the value one in it. Then add an append operator with two and three in a variadic list and another append operator with four as the only member of the variadic list. Run the example. You'll see one, two, three, and four printed out to the console. Like prepend, completion events come into play here. The first publisher must send a completion event, so the operator knows when to start appending values. Change the original publisher to a pass through subject and send values one and two through that publisher. Run the example. The operators will never amend values until a completion event comes from the original publisher. Add that completion event and rerun the example. The console will display the sequence of numbers again, since the operators knew when to start emitting their values. Just like with prepend, sequences conformable collections can be appended to publishers. This includes arrays, sets and strides. Make an example stub with an array containing one, two and three in the initial array. Add three append operators, one with an array containing four and five, another with a set initialized from an array containing six and seven, and the last one with a stride that generates every other integer from eight to 11. Run the example. This will print one through eight, followed by 10, to the console. Finally, as with prepend, append can take publishers and append all of their meta values onto the original publisher. Make an example stub with two publishers, one from an array containing one and two, and another from an array containing three and four. Add an append operator with a publisher two as the argument, and run the Playground. As expected one, two, three, and four, get printed to the console. In this episode, we looked at appending operators, which add emissions from one publisher after the original publisher. The append(Output) operator takes a variadic list of values, as long as they are the same type as the original publisher. The original publisher must send a completion event, so the operator knows when to start emitting its values. The append(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 append(Publisher) operator emits the values from the passed in publisher before emitting the values from the original publisher. We've covered prepending and appending operators, and it looks like, yes, a challenge is up ahead. Go take a quick break and come back to test your knowledge on prepending and appending operators.