Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rxjava2 distinctUntilChanged() not working properly after first time

I am trying to use .distinctUntilChanged() and its not passing value to switchmap() after first time.

RxTextView.textChanges(etUserQuery).debounce(300, TimeUnit.MILLISECONDS)
    .observeOn(AndroidSchedulers.mainThread()).filter(charSequence -> {
        if (charSequence.toString().isEmpty()) {
            etUserQuery.setHint("Please type username");
                return false;
            } else
                return true;
        }).distinctUntilChanged()
        .switchMap(charSequence -> vm.dataFromNetwork(charSequence.toString()))
        .subscribe(fetchUserResponce -> {
            noDataText.setVisibility(fetchUserResponce.getItems().size() == 0 ? View.VISIBLE : View.GONE);
            mUsersListAdapter.updateData(fetchUserResponce.getItems());
        }));

Is it correct place to use .distinctUntilChanged()?

like image 902
Ashraf Patel Avatar asked May 03 '26 23:05

Ashraf Patel


1 Answers

From the comments:

The CharSequence the textChanges gives you is mutable and should be turned into String before it is passed along.

textChanges gives you the same CharSequence reference and distinctUntilChange relying on Object.equals() will find it equal to itself all the time.

like image 108
akarnokd Avatar answered May 05 '26 13:05

akarnokd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!