Filtering Operators in Combine Framework

Nilaakash Singh
Dev Genius
Published in
4 min readMar 5, 2022

--

In this article we are going to talk about filtering operators of Combine Framework in iOS, these operators provide ways to transform data just like collection operators do, but instead they work on a series of data.

Use Cases: It is a common programming task to filter sets of data, the same applies to sequences of data. The Combine framework offers many ways to map, filter and reduce sequences of data, these operations are chained together and a transformation is applied to each element as it passes through the operators, rather than doing the operation on sets of data.
I would like to introduce some of the operators in this article and invite you to explore many more that is provided by Combine.

Prerequisites:

  • Xcode 11 and above which supports Combine framework
  • Basic knowledge of Publishers, Subscribers, and Subjects.

Let's get started:

Filter:

Filter in combine works in the same way as Filter in swift. The only difference here is we can apply a filter even before receiving the value.
There are two variations of the filter as seen in the code snippet below:

Filter Filtering Operator in Combine Framework.

In the above snippet, we have a common publisher on Line 1 which is used to subscribe and filter conditions for both types of filter.

- For filter we can directly add conditions in closures and sink the result.
- For tryFilter we throw the last number exception just for learning purpose and if you observe sink we have receive completion and receive value block. It is required because our filter can also throw exception.

Compact Map:

Compact Map is a filtering operator used to map the data as per operation. We also have a map in combine but it is part of the Transforming operator. Compact map removes nil elements from the data and publishes non-nil elements when subscribed.
We also have a variety of compact map named as tryCompactMap which can throw errors when required. You can read more about this variation in Apple Documentation.

Compact Map Filtering Operator in Combine Framework.

In the above example, We intend to make all the strings in the array as int and then print it. We can easily do it compact without creating an extra variable and then subscribe to it to print the result.
As we can see you will not find the result for “None” in the output because that will be nil and compact map will skip the stream.

Ignore Output:

Ignoring Output is a very special kind of operator which ignores all the upstream data but passes the completion state of the publisher. This can be helpful for silent API calls, notifications, or background database sync work where we only need the completion state.

Ignore Output Filtering Operator in Combine Framework.

In the above example, we have made a publisher on Line 2 and we are ignoring output at Line 4. This will not print anything in receive value block but at the end, it will print completion in receive completion block.

First and Last:

First and Last is also the same as Swift First and Last operator. The only difference is, We will find more variations of first and last in swift compared to Combine.

First and Last Filtering Operator in Combine Framework.

In the above example we have used First and Last operators on the subscriber which will give results as “1” and “100” respectively. There is variation in first and last called as “where”. It will return the first/last element which will satisfy the “where” condition.

Drop First, Drop While, Drop Until Output From:

Drop operator in combine omits the elements in the upstream until the closure result is satisfied. It doesn't delete the element from memory like we observe in “remove”.
There are three variations of drop operator:

  1. Drop First:
    Drop First skips the elements from the start till the expression is satisfied. We can use this operator whenever we want to skip initial data.
Drop First Filtering Operator in Combine Framework.

In the above example, We can see the simplest we can use drop first operator it will skip the first 2 elements and will print the remaining elements when published.

2. Drop While:

Drop While can be used to skip elements with expression. This can be used for showing dataset such as odd, even, multiple, divide, less than, greater than etc.

Drop While Filtering Operator in Combine Framework.

In the above example, we have used drop while operator on Line 6 with a less than condition and when subscribe it, the output will be result of skipped elements that satisfies the condition.

3. Drop Until From:

Until From is an interesting operator wherever we use it, It takes two subscribers, one for subscribing and one for tracking the subscription.

Drop Until From Filtering Operator in Combine Framework.

In the above example, We have used 2 subscribers the main subscriber is drop subscriber which is used for subscription. The stopping subscriber is passed on Line 9 defining it as subscriber which can stop subscription. We are stopping the subscription on Line 15 as per condition.

Prefix:

Prefix can be used to limit the number of data required from a publisher. The can be helpful in pagination's or in cases where we only need a certain amount of data.
There are many variations of a prefix such as while, maxLength, untilOutputFrom, tryPrefix which you can easily find in Apple Documentation.

Prefix Filtering Operator in Combine Framework.

In the above example, we are only using a normal variation of a prefix which will give us the first 7 elements as output.

Note: There is one more filter operator named as “Remove duplicates”. However, I will write about it later.

That's all folks with Sequence Operators in Combine. I’ll be back with different combine operators in the next blog.

Where to go from here:

We can try this operators at a time when we are using subscribers to fetch data from the database or server, this will help us to get filtered data and avoid data which is not required at the beginning. You can find different combine operator and their usage in my GitHub repository.

More Reads:
Sequence operators in Combine: https://nilaakash.medium.com/sequence-operators-in-combine-framework-c942182150a

Learning RxSwift and RxCocoa: https://medium.com/@iAkashlal/learning-rxswift-and-rxcocoa-basics-with-snippets-e282530d10e7

Share this blog to spread the knowledge | Do-Follow, and CLAP if you Liked it! Happy Coding :)

Add me on Linkedin.

--

--

Senior iOS Engineer at Loblaw Digital | Swift | SwiftUI | Combine | Swift Async | Blogger.