Reactive Programming in iOS with Combine

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

Part 4: Timing, Scheduling and Sequencing Operators

28. Sequencing 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: 27. Scheduling Operators Next episode: 29. Challenge: Sequence Operators

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.

On their surface, publishers are really just sequences and a set of sequence operators and combine lets you work with the sequence as a whole, instead of individual values. As we go through these operators, you'll notice that many have the same names as functions from the Swift standard library but be sure to note any differences between those functions and the operators in Combine when you use them in your code. The min operator, which is greedy, lets you find the minimum value emitted by publisher. It has to wait for the publisher to send a .finished completion event, so it knows there are no other values that could possibly change what it considers to be the minimum value. You can see this in the marble diagram, nothing gets admitted to the downstream consumer until the completion event, represented here by the vertical line, gets sent. In this case -50, the minimum value from the publisher gets sent. Let's take a look at this in a playground set up an array with the values, 1, -50, 246 and 0 and use the publisher method on the array to get a publisher we can use in the example. Then build the publisher operators, sync pipeline adding a print operator to help see what's happening during the execution of the pipeline. run the playground In the console. You'll see that the operator takes an all of the values from the publisher before determining the minimum value reflecting the operator's greediness. As you might imagine, if combined has a min operator it has to have a max operator. You would also be right if you guess that instead of returning the minimum value from the publisher, it returns the maximum value. Max is also a greedy operator requiring all the values from the publisher before it can determine the max value. Let's look at that in a demo, make an array with strings A, F, Z and E, and use the publisher method on the array to get a publisher we can use in this example. Then build the publisher operator sync pipeline, adding in a print operator to help see what's happening during the execution of the pipeline. Run the playground. In the console, you'll see that max like min waits for all the values to be emitted by the publishers before determining the max value. Something to take note of here is that strings and integers are comparable in Swift. So there was no need to specify how to determine if a value is smaller or larger than another value. If you have your own value that doesn't adopt comparable, min by and max by can be used to pass in a closure to help the operator determine which value to return. The count operator does exactly that, it counts the number of values admitted by the publisher. Again, this operator's greedy, since it has to get all the values before it can determine how many values were admitted by the publisher. Let's take a look, make an array with the strings, A, B, and C, and use the publisher method on the array to get a publisher we can use in this example. Then build the publisher operators in pipeline, adding in, a print operator to help see what's happening during the execution of the pipeline. Run the playground. In the console, you'll see that I have three items as printed once all of the values have been admitted by the publisher. Finally, let's look at the output operator. This operator will only return values that were admitted at specific indices in the upstream publisher. There are two varieties. Output out, returns the value at the specified index as you can see in the marble diagram. Output in, returns values at a specific range of indices. In the marble diagram, the requested indices are one through three inclusive. So the values B, C and D are passed to the downstream consumer. Let's look at both of these in a demo. Make an array with the strings, A, B, and C, and use the publisher method on the array to get a publisher we can use in this example. Then build the publisher operator sync pipeline, adding in, a print operator to help see what's happening during the execution of the pipeline. Run the playground. In the console, the print statement appears when index one is reached stating that value at index one is B which is correct. To examine the behavior of output in, making array with the strings A, B, C, D and E and use the publisher method on the array to get a publisher we can use in this example. Then build the publisher operator, sync pipeline adding in a print operator to help see what's happening during the execution of the pipeline. Run the playground. In the console, you'll see statements for the values B C and D corresponding to indices one two and three from the original publisher. In this episode, we looked at a series of sequence operators in Combine, the min operator returns, the minimum value the publisher produces, the max operator returns, the maximum value the publisher produces, the count operator returns, the number of emitted values of the publisher and the output operator only returns values at a specific index or a range of indices of the publisher. If I look at what's next in our sequence of topics it's a challenge. When you're ready to test your knowledge about sequencing operators, hop over to the next episode.