0

I have been trying to implement this feature where I have to search for every keyword the user types but I have this limitation that I can hit the server only after 1 sec of the previous call. So if I'm typing a, b, c, d the hit should work like that a --- 1 second interval--- ab --- 1 second interval--- abc --- 1 second interval--- abcd

I tried using debounce, delay( caused looper exception ). Can somebody tell me how can I achieve it using RxJava(Android).

public Observable<String> userTypings() {
    return RxTextView.textChanges(inputText).skip(1).flatMap(new Func1<CharSequence, Observable<String>>() {
        @Override
        public Observable<String> call(CharSequence charSequence) {
            return Observable.just(charSequence.toString());
        }
    });
}

This is how the emission of events is being done in View of the MVP pattern. Comment if you need anything else!

codeyourstack
  • 341
  • 1
  • 2
  • 11

1 Answers1

0

There are a couple of approaches to this, one using publish() and a mix of sample and debounce to get the timing right.

The other way is a ObservableConflate: this observable was developed to handle exactly this issue.

keyboardInputObservable
  .compose(f -> Observable.create(new ObservableConflate<Integer>(f, 1, TimeUnit.SECONDS, scheduler) ) )
  .doOnNext( input -> lookup(input) )
  ...

The operator will limit the rate of requests to lookup to one per second.

Note: the create() operator used there may need to be unsafeCreate() in some library versions.

You can thank @akarnokd for the solution.

Bob Dalgleish
  • 8,167
  • 4
  • 32
  • 42