Implement Search Using RxJava Operators

Nowadays, most of the applications that we use in our daily life come with a search feature which provides us a facility to get things very quickly we are looking for.

So, having a search feature is very important. And, we as a developer have a responsibility to implement it in a better way.

Let’s see how to implement it in a better way using the RxJava Operators.

Don’t forget: There is an operator for everything in RxJava.

I personally believe that we can solve any complex problem very easily with RxJava which can be very difficult without RxJava. RxJava is just awesome.

The following are the things of RxJava that we will be using to implement this search feature:

  • Publish Subject: I have provided the simplest introduction to the RxJava Subject, please refer this article to know more about the Subject in RxJava.
  • Filter Operator
  • Debounce Operator
  • DistinctUntilChanged Operator
  • SwitchMap Operator

Wait! we will discuss all the above one by one.

Let’s begin

First of all, you will have to make the SearchView observable. Let’s make the SearchView observable by using the PublishSubject. I am using the Android SearchView. The view can be anything like EditText. It is just that you will have to make that view observable by implementing the text change listener.

Then, on that SearchView observable, you will have to apply all the operators like below:

Now, it’s time to learn why these operators are being used and how they work in combination.

  • Debounce: Here, the debounce operator is used with a time constant. The debounce operator handles the case when the user types “a”, “ab”, “abc”, in a very short time. So there will be too much network calls. But the user is finally interested in the result of the search “abc”. So, you must discard the results of “a” and “ab”. Ideally, there should be no network calls for “a” and “ab” as the user typed those in very short time. So, the debounce operator comes to the rescue. The debounce will wait for the provided time for doing anything, if any other search query comes in between that time, it will ignore the previous item and start waiting for that time again with the new search query. If nothing new comes in that given constant time, it will proceed with that search query for further processing. So, debounce only emit an item from an Observable if a particular timespan has passed without it emitting an another item.
  • Filter: The filter operator is used to filter the unwanted string like empty string in this case to avoid the unnecessary network call.
  • DistinctUntilChanged: The distinctUntilChanged operator is used to avoid the duplicate network calls. Let say the last on-going search query was “abc” and the user deleted “c” and again typed “c”. So again it’s “abc”. So if the network call is already going on with the search query “abc”, it will not make the duplicate call again with the search query “abc”. So, distinctUntilChanged suppress duplicate consecutive items emitted by the source Observable.
  • SwitchMap: Here, the switchMap operator is used to avoid the network call results which are not needed more for displaying to the user. Let say the last search query was “ab” and there is an ongoing network call for “ab” and the user typed “abc”. Then you are no more interested in the result of “ab”. You are only interested in the result of “abc”. So, the switchMap comes to the rescue. It only provides the result for the last search query(most recent) and ignores the rest.

Returns a new Observable by applying a function that you supply to each item emitted by the source Observable that returns an Observable, and then emitting the items emitted by the most recently emitted of these Observables.

Yeah, we have done it. Just imagine how much complex the search implementation would have been without RxJava.

That’s it for now.

If you want to check the complete example, just clone the project and check the search implementation.

Thank you so much for your time.

Happy Learning 🙂

Also, Let’s become friends on Twitter, Linkedin, Github, and Facebook.

Amit Shekhar

Amit Shekhar

Co-Founder at Mindorks | Learning is a journey, let’s learn together