Implement Search Using RxJava Operators
Last Updated :
11 Aug, 2021
Nowadays, most of the programs we use in our everyday lives include a search option that allows us to easily find what we're looking for. As a result, having a search tool is critical. And it is our job as developers to implement it better. Let's look at how to do it better with the RxJava Operators.
Image 1. The RxSearch as in GfG.
The following RxJava features will be used to build this search feature:
Prerequisite: We will need to know the basic RxJava functions, as implementing search is somewhat high order.
Whoa! although this may sound like implementing a search is really tough, but NO. It's really easy when we do it stepwise as mentioned below in this article, and you will be up and running in no time. Moreover, all the terms above are linked to other awesome GfG articles to extend your learning! Without further ado, let's start.
Step by Step Implementation
Step #1: Making search view Observable
First and foremost, you must make the SearchView visible. Let's use the PublishSubject to make the SearchView observable. Here we are making use of the Android SearchView. The view may be anything that looks like EditText. It's only that you'll need to build the text change listener to make that view visible.
Java
public class GfGSearch {
public static Observable<String> fromView(SearchView gfgSearch) {
final gfgArticle<String> article = gfgArticle.create();
gfgSearch.setOnQueryTextListener(new gfgSearch.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
article.onComplete();
return true;
}
@Override
public boolean onQueryTextChange(String text) {
article.onNext(text);
return true;
}
});
return article;
}
}
Step #2: Applying the Operators
You must use the following operations on that SearchView observable:
Java
@Override
public ObservableSource<String> apply(String query)
{
return dataFromNetwork(query)
.doOnError(throwable
-> {
// Exception Handling
})
.onErrorReturn(throwable -> "");
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<String>() {
@Override public void accept(String result)
{
searchResult.setText(result);
}
});
RxGfGSearch.fromView(searchView)
.debounce(200, TimeUnit.MILLISECONDS)
.filter(new Predicate<String>() {
@Override public boolean test(String text)
{
if (text.isEmpty()) {
searchResult.setText("Spandan");
return false;
}
else {
return true;
}
}
})
.distinctUntilChanged()
.switchMap(new Function<String, ObservableSource<String> >() {
Understanding the Terms which we used in the above code:
DistinctUntilChanged: The distinctUntilChanged operator prevents repeated network calls. Assume the most recent ongoing search query was “abc,” and the user erased “c” before typing “c” again. So it's "abc" once more. So, if a network call is already in progress with the search query "abc," it will not initiate a duplicate call with the search query "abc." As a result, distinctUntilChanged prevents the source Observable from emitting duplicate successive items.
- Filter: The filter operator is used to filter out undesirable strings, such as the empty string in this example, in order to avoid making an unnecessary network call.
- Debounce: The debounce operator is employed with a time constant in this case. When a user inputs “a”, “ab”, or “abc” in a brief period of time, the debounce operator handles the situation. As a result, there will be an excess of network calls. However, the user is ultimately interested in the result of the search "abc." As a result, the outcomes of “a” and “ab” must be discarded. Ideally, no network calls for “a” and “ab” because the user wrote those in a very short period of time. As a result, the debounce operator steps in to help. If another search query arrives in between that time, the debounce will discard the old item and start waiting for that time again with the new search query. If nothing new arrives within that constant time period, it will return to that search query for additional processing. As a consequence, debounce will only emit an item from an Observable if a certain timespan has passed without another item being emitted.
- SwitchMap: The switchMap operator is used here to prevent network call results that are no longer required for showing to the user. Assume the most recent search query was "ab," there is an active network call for "ab," and the user entered "abc." Then you're no longer interested in the outcome of "ab." You are just concerned with the outcome of "abc." As a result, the switchMap comes to the rescue. It only returns the results of the most recent search query and ignores the others.
Geek Tip #1: Returns a new Observable by applying the given function to each item emitted by the source.
Conclusion
And yes, that's pretty much it for this article, by this you would now be able to implement a RxSearch using RxJava, this search is used in many places throughout like Netflix, Google, and even Geeks for Geeks. Imagine how tough the world would be without Rx!
Always Remember:
There is something for everything in RxJava!
Similar Reads
Implement Caching in Android Using RxJava Operators
The cache on your Android phone is a collection of little pieces of information that your apps and web browser utilize to improve efficiency. RxOperator is essentially a function that specifies the observable, as well as how and when it should emit the data stream. In RxJava, there are hundreds of o
5 min read
Understanding RxJava Zip Operator With Example
According to the official RxJava documentation "Zip combines the emissions of several Observables using a given function and emits single items based on the outcomes of this function for each combination". The zip operator enables us to obtain results from several observables at the same time. Image
3 min read
Understanding RxJava Timer, Delay, and Interval Operators
We will learn about the RxJava Timer, Delay, and Interval Operators in this post. Also, the use case of all the different Timers, Delays, and Interval Operators would be clarified in this article so stay tuned till the end. With examples, we shall study all of the following operations. TimersDelaysI
3 min read
RxJava Defer Operator
In this article, we will learn about the RxJava Defer Operator. Depending on our use case, we'll know when to utilize the Defer operator. We frequently make mistakes when utilizing the RxJava Defer Operator. Let's get this straight so we don't make a mistake. According to the documentation: Defer: w
2 min read
Understanding RxJava Create and fromCallable Operator
In this article, we will learn about the RxJava Create and fromCallable Operators. We can choose between the required function based on what is required skillset is needed. We frequently make mistakes when utilizing RxJava Operators. Let's get this straight so we don't make a mistake. With examples,
2 min read
Operator Overloading in MATLAB
MATLAB allows you to specify more than one definition for an operator in the same scope which is called Operator Overloading. We can redefine or overload most of the built-in operators available in MATLAB. It is basically a type of polymorphism in which an operator is overloaded to give user-defined
3 min read
RxJava Operator - Concat and Merge
RxJava is the most significant library, and it is widely used by Android developers. It simplifies our lives. RxJava is used for multithreading, managing background processes, and eliminating callback hells. RxJava allows us to address a wide range of complicated use-cases. It allows us to accomplis
3 min read
How to Implement EventBus With RxJava - RxBus?
Prior to RxJava, we used Bus libraries like Otto, EventBus, startActivityForResult Intent, and others to update other fragments on behalf of the current job. Let's say we started the first activity, then another, then another, and so on until the fourth activity is in the foreground and the other th
3 min read
Operator Overloading in Julia
Operator overloading in Julia refers to the ability to define custom behavior for built-in operators such as +, -, *, /, etc. when applied to objects of custom types. This allows users to create intuitive and concise code that works with custom data types as if they were built-in. In Julia, operator
5 min read
Solidity - Assignment Operators
Solidity is a high-level, statically-typed programming language for Ethereum smart contracts. Python, JavaScript, and C++ impact it. Solidity has several variable and value assignment operators. Solidity supports the following types of operators: Simple Assignment Operator.Arithmetic Assignment Oper
5 min read