Reactive Programming in iOS with Combine

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

Part 2: Transforming & Filtering Operators

10. Filtering 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: 09. Challenge: Create a Phone Number Lookup Next episode: 11. More Filtering 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.

Filtering operators consume values from a publisher and conditionally decide whether to pass them onto the consumer. There are quite a few filtering operators to talk about and even variations on those operators. So I've already run my own filtering operator to narrow them down to a set to go over here. Let's get started with some filtering basics. Let's start with the happily named filter operator which takes a predicate to determine if the value gets passed on to the consumer. The marble diagram here shows that a series of numbers one through 10 are sent via the publisher, but the predicate only returns true for those that are multiples of three, leaving three, six and nine. Only those three values are sent to the consumer on the bottom line. Let's see how that looks on a playground. Add the following to the starter playground. Here you create a new publisher which will emit a finite number of values, one through 10 and then complete using the publisher property on sequence types. And you use the filter operator passing in a predicate where you only allow through numbers that are multiples of three. Sometimes you may want to filter duplicate values coming in from your publisher before sending them downstream. That's easy enough to do with the remove duplicates operator and (inaudible) this operator is really easy. You don't need to pass any arguments. As long as the values in the stream conform to equatable. Let's look at that in a demo. Add the following to the playground. This ampersand operator is straight forward. Insert the operator between the publisher and a subscriber and just like that duplicates get filtered out. Some publishers can emit optional values or even just return nils as a result. But what if you don't want those values? Those of you familiar with the Swift Standard Library may recognize compact map as a solution here and as luck will have it, there is an operator that does the same thing. The marble diagram here shows that the values on the incoming stream that can be cast as floats get passed onto the consumer. But those that can't like the letter A get filtered out. Let's look at that in the playground. Add the following to the playground, here you create a publisher that emits a finite list of strings, use compact map to attempt to initialize a float with those strings. If floats initializer can do that it returns nil and each nil is filtered out and the sink subscriber receives and prints only the correctly initialized floats. There may be cases where you just want to ignore all of the values from a publisher. Only being concerned when the completion event is sent, the ignore output operator does this for you as shown in the marble diagram. Only the completion event is passed downstream. Let's see how this looks in code. Add the following to the playground, here you initialize a publisher that emits values one through 10,000. However you attach the ignore operator to that publisher and a sink subscriber onto the end of the pipeline. There's no real surprise here that the only thing printed when you run the playground is completed with finished. That covers some of the basics of filtering operators. You have learned the basic filter operator which uses a predicate to exclude values from getting sent to the consumer, remove duplicates and ignore output which do exactly what they say they do, remove duplicates from the stream and ignore output from the publisher entirely up to the completion event. And compact map which handles cases where the upstream publisher sends some values that are nil which is just like compact map from the Swift Standard Library. In the next part of the course, you'll see some additional operators that let you horn in on just the values you need and if need be cancel the publisher instead of waiting for all the values to get omitted.