dilix
dilix

Reputation: 3893

delay + distinctUntilChanged in RXjava with RXBindings

I'm trying to monitor text changes with some delay to avoid spam from listener. But of course I don't want to receive some items that are already handled.

This observer

RxTextView.textChanges(editText)
                .delay(2, TimeUnit.SECONDS)
                .distinctUntilChanged()
                .filter(charSequence -> charSequence.length() != 0)
                .subscribe(charSequence1 -> Log.e("!@#", charSequence1));

emmits such an items when I enter "abcd":

E/!@#: abcd
E/!@#: abcd
E/!@#: abcd
E/!@#: abcd

So I receive the emmited items 4 times but the strings are equals and there is distinctUntilChanged. Why distinctUntilChanged not works in this case? Is it possible to achieve this logic with delay with rx operators?

Upvotes: 1

Views: 1148

Answers (1)

akarnokd
akarnokd

Reputation: 69997

If I remember correctly, textChanges or the text control reuses the same CharSequence so you get this kind of anomaly. You have to use map(v -> new String(v)) before the delay to create an immutable copy.

Is it possible to return only last result when user stop typing? In this case abcd for one time? last() not for this issue as I understand.

Look for examples of the debounce operator.

Upvotes: 10

Related Questions