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.

Transcript: 16. Append

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 that not all sequence 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. Setup the example(of) stub, and have the original publisher come from an array with the value 1 in it. Then, add and append operator with 2 and 3 in a variadic list, and another append operator with 4 as the only member of the variadic list. Run the example. You’ll see 1, 2, 3, and 4 printed out to the console.

example(of: "append(Output...)") {
  // 1
  let publisher = [1].publisher

  // 2
  publisher
    .append(2, 3)
    .append(4)
    .sink(receiveValue: { print($0) })
    .store(in: &subscriptions)
}

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 PassthroughSubject, and send values 1 and 2 through that publisher. Run the example. The operators will never emit 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.

example(of: "append(Output...) #2") {
  // 1
  let publisher = PassthroughSubject<Int, Never>()

  publisher
    .append(3, 4)
    .append(5)
    .sink(receiveValue: { print($0) })
    .store(in: &subscriptions)

  // 2
  publisher.send(1)
  publisher.send(2)
  //publisher.send(completion: .finished)
}

Just like with prepend, Sequence-conformable collections can be appended to publishers. This includes arrays, sets and strides. Make an example stub with an array containing 1, 2, and 3 in the initial array. Add 3 append operators: 1 with an array containing 4 and 5, another with a Set initialized from an array containing 6 and 7, and the last one with a stride that generates every other integer from 8 to 11. Run the example. This will print 1 though 8, followed by 10, to the console.

example(of: "append(Sequence)") {
  // 1
  let publisher = [1, 2, 3].publisher

  publisher
    .append([4, 5]) // 2
    .append(Set([6, 7])) // 3
    .append(stride(from: 8, to: 11, by: 2)) // 4
    .sink(receiveValue: { print($0) })
    .store(in: &subscriptions)
}

Finally, as with prepend, append can take publishers and append all of their emitted values onto the original publisher. Make an example stub with 2 publishers - one from an array containing 1 and 2, and another from an array containing 3 and 4. Add an append operator with publisher2 as the argument, and run the playground. As expected, 1, 2, 3, and 4 get printed to the console.

example(of: "append(Publisher)") {
  // 1
  let publisher1 = [1, 2].publisher
  let publisher2 = [3, 4].publisher

  // 2
  publisher1
    .append(publisher2)
    .sink(receiveValue: { print($0) })
    .store(in: &subscriptions)
}


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.