1

I'm using RxBinding2 to listen for text changes in SearchView. I need to use separate logic with different debounce when user types smth in EditText. I tried this:

    RxSearchView.queryTextChanges(searchView)
            .debounce(500, TimeUnit.MILLISECONDS) // use debounce 
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(query -> mPresenter.searchRequest(query));
    RxSearchView.queryTextChanges(searchView) // no debounce required here
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(query -> mPresenter.updateUI(query));

RxSearchView.queryTextChanges overrides listener, so, of course, only second being called.

May I combine these two calls searchRequest and updateUI with different debounces into one rx-sequence? Should I use filter operator in someway?

Andrey Aleev
  • 192
  • 1
  • 1
  • 13

1 Answers1

1

You can solve this in many ways, but I think the cheapest way would be to use the .share() operator. This operator is a shorthand for .publish().refCount(). So you could do something like this.

Observable<String> sharedTextChanges = RxSearchView.queryTextChages(searchViw).share()

sharedTextChanges
            .debounce(500, TimeUnit.MILLISECONDS) // use debounce 
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(query -> mPresenter.searchRequest(query));

sharedTextChanges
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(query -> mPresenter.updateUI(query));

Hope this helps.

Lukasz
  • 2,257
  • 3
  • 26
  • 44