What Is Debounce Operator In RxJava

Posted By : Daljeet Singh | 30-Apr-2018

RxJava provides us with a wide variety of operators to make our code cleaner and simpler. An operator simply modifies the data streams emitted by an observable before they are consumed by an observer. Using an operator, one can filter, merge or group streams of data. While operators like map, flat map and filter are used pretty often to manipulate the data streams emitted by an observable, one particular operator that can be used in the implementation of a quite common use-case is debounce.

 

The debounce operator only emits an item from an observable if a particular period of time has elapsed between the previous emission and now. So what this essentially means is, if the debounce interval for a particular observable is set at 500ms, the debounce operator will wait for a time period of 500ms after an item has been emitted by the observable, before sending this item to the observer.

 

Now, if another item is emitted by the observable inside this time period of 500ms, the debounce operator will reset the time interval back to 500ms and wait to deliver the new item to an observer. Here the previously emitted item by the observable won't be delivered to the observer. After an item is delivered to the observer, the debounce interval is reset to 500ms and a newly emitted item will only be delivered after this time period.

 

The debounce operator can be used in implementing instant search inside our android application. In case of instant search, the user is provided search results as he/she starts typing. Typically, this is achieved by sending a request to query our local database/server for every character that a user types in. This process has a significant overhead as many of the results returned upon typing the initial query characters are not too useful for the user.

 

Using debounce for instant search would reduce the number of requests sent to our local DB/server as we can set a time period after which a query is emitted. For instance, a user maybe type "oodles" in a timespan of 500ms and if we have set a debounce time period of 500ms to our query, this would result in a single query being sent to the database, while without the use of debounce, a query may have been sent six times(number of characters) to the database.

 

A sample code snippet for the same would be :

public class DebounceSearchActivity extends AppCompatActivity {
 
    private Disposable disposable = new Disposable();
    private Unbinder unbinder;
 
    @BindView(R.id.et_input_query)
    EditText inputQuery;
 
    @BindView(R.id.tv_search_string)
    TextView txtSearchString;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_debounce_operator);
        unbinder = ButterKnife.bind(this);
 
        disposable.add(
                RxTextView.textChangeEvents(inputQuery)
                        .skipInitialValue()
                        .debounce(500, TimeUnit.MILLISECONDS)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribeWith(searchQuery()));
    }
 
    private DisposableObserver<TextViewTextChangeEvent> searchQuery() {
        return new DisposableObserver<TextViewTextChangeEvent>() {
            @Override
            public void onNext(TextViewTextChangeEvent textViewTextChangeEvent) {
                Logger.LogError("Status", "search string: " + textViewTextChangeEvent.text().toString());
            }
 
            @Override
            public void onError(Throwable e) {
 
            }
 
            @Override
            public void onComplete() {
 
            }
        };
    }
 
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        disposable.clear();
    }
}

 

About Author

Author Image
Daljeet Singh

Daljeet has experience developing android applications across a range of domains such as Cryptocurrency, Travel & Hotel Booking, Video Streaming and e-commerce. In his free time, he can be found playing/watching a game of football or reading up on either

Request for Proposal

Name is required

Comment is required

Sending message..