Filtering Operators In RxSwift

Umair Ishrat Khan
6 min readDec 6, 2021
RxSwift

What is filtering? or what does filter mean? Well we all have heard a term “Water Filter”.

The definition of a water filter is as follow “A water filter removes impurities by lowering contamination of water using a fine physical barrier, a chemical process, or a biological process.” ok too much of chemistry there I guess. In simple words a water filter will remove all the unwanted impurities from water by using some process. Similarly filtering operation removes the unwanted items from the sequence for us.

Photo by Najib Kalil on Unsplash

We are going to discuss 9 types of filters:

  • Filter
  • Ignore
  • ElementAt
  • Skip
  • skipWhile
  • skipUntil
  • Take
  • takeWhile
  • takeUntil

Filter :

This operation can be used in two ways either you tell what you want or you tell what you don’t want from the sequence.

RxSwift Filter

On the image above I am telling the filter out of the observable I only want the value that is equal to 5. The $0 is each element of the observable the filter function iterates over each element check if that element meets our condition or not. If yes it passes it to us else it ignores it.In my print console you can see the values printed are only 5 & 5. Let me try to explain it with better graphics.

Filter operation in RxSwift

Our condition was only give us the value if it is equal to 5. The filter operator iterated over the sequence checked each value against our provided condition only when the condition met it turned the red ‘X’ to a green tick.

Similarly now if I apply a filter with condition $0 != 5 what values do you think will get printed.

It will print all the values except for 5 lets see a practical implementation.

Filter in RxSwift

Can you see 5 printed ?? No! because our condition is telling the filter to give us only the values which are not equal to 5.

Ignore:

Ignore filter is something that will ignore all the events in a sequence but still will trigger onCompleted means it will trigger the completed event.

As you can see our emoji is ignoring all the elements but he is happy as soon as he meets Completed event and triggers it self. Lets have a look at practical implementation.

ignore in Rxswift

I am using a count variable and incrementing it by one for every event. Notice when I used ignoreElements() it only triggered once thats only when it gets onCompleted event. Lets see without .ignoreElements()

ignore in Rxswift

See it triggered all 5 times. So its good to use ignoreElements when all you care about is completed event.

ElementAt:

This filter will return you the element at specified index.

Lets have a look at practical implementation.

RxSwift ElementAt

So it just printed 2 because we passed the index of value 2.

Skip:

The skip operator will simple skip the number of values for the parameter passed it to. It takes an Int as parameter and will keep skipping the values for the number of Int passed e.g if you’ve passed 2 it will skip first two values. If you’ve passed 4 it will skip first four values.

Skip in RxSWift

As we passed 1 it skipped the very first value of sequence ‘newSubject’. Similarly if I pass 2 it will skip the first two values and only 3 & 4 will be printed.

SkipWhile:

This function will keep skipping the values until a specific condition is met (condition that we pass into it). Lets say I’ve following observable:

let observable = Observable.of(2,3,4,5,6,7,8,9,10)

and I’ve passed a condtion of skip(until: $0 % 2 == 0). What do you think it will print

It will print all the items after 2 because the second element of sequence is breaking our condition. Once the condition is broke then it won’t check for any conditions again.

Let’s say I’ve following observabel:

let observable = Observable.of(1,2,3,4,5,6,7,8,9,10)

What do you think when apply the same skipWhile I applied above on this observable. It will print all the values because our first element is breaking the condition.

RxSwift SkipWhile

SkipUntil:

SkipUntil will keep skipping the values until a specified condition is not met. It is very similar to skipWhile except for that the condition is provided by another observable.

SkipUntil in RxSwift

As you can see above the subscriber ignored all the elements before tirgger.onNext(). Becuase thats what our skipUntil is telling the subscriber to do. As soon as we provided the trigger the subscriber started to provide us with the values since the skipUntil condition was met.

Take:

This operator will only take the number of items passed to it as paramneter.

Take Operator RxSwift

We passed 2 as parameter and it will only take the first 2 values now. If we would’ve passed 3 then it would’ve taken first three values. Lets have a look at practical implementation.

Take RxSwift

TakeWhile:
Consider it as opposite of skipWhile in skipWhile when we passed the condition of {$0 % 2 == 0} it skipped the values whose modular was 0 until the condition was broken by someone whose modular is not 0 in takeWhile if we pass the same condition it will skip the number whose modular when divided by 2 is not 0 and give us the numbers whose modular is 0.

let say I’ve a following Observable:

let observable = Observable.of(2,4,6,3,8,10)

if I pass this condition {$0 % 2 == 0}

it will give 2 ,4,6 as soon as $0 = 3 the condition will break and we won’t get anyother values no matter how many more values we have who are satisfying our condition.

TakeUntil:

Yes! its the opposite of skipUntil in skipUntil we use to listen to events after another observable is triggered but here we stop listening as soon as another observable is triggered.

Take a look at practical implementation for better understanding.

After the trigger observable was trigger we are not receiving any other values.

Hope my this article helped you understanding the filtering operators in RxSwift. If you like my articles don’t forget to follow me I’ll be posting more articles related to Swift, RxSwift and Data Science.

--

--

Umair Ishrat Khan

An energetic and motivated individual IOS developer/ Data Science Practitioner. Apart from computer science Martial arts interests me the most.