Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why debounce() with toList() doen't working in RxAndroid?

While I'm using debounce() ,then fetch data from backend and the data I want to convert to another data and lastly use toList(). when I'm using toList() nothing happens no any log not in subscribe and error ,without toList() it works and subscribe() method enters as much as I have list of books, I tested the second part of code it without debounce() just getItems() and using toList() it works. Below is my code the first part with debounce() and itList() which is not working and the second with toList() which works

public Flowable<List<Book>> getItems(String query) {}

textChangeSubscriber
            .debounce(300, TimeUnit.MILLISECONDS)
            .observeOn(Schedulers.computation())
            .switchMap(s -> getItems(s).toObservable())
            .flatMapIterable(items -> items)
            .map(Book::convert)
            .toList()
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(books -> {
                Log.i("test", "" + books.toString());
            }, error -> {
                Log.i("test", "" + error);
            });


   getItems(query).flatMapIterable(items -> items)
            .map(Book::convert)
            .toList()
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(Schedulers.io())
            .subscribe(books -> {
                Log.i("test", "" + "" + books.toString());
            }, error -> {
                Log.i("test", "" + error);
            });
like image 568
I.S Avatar asked Apr 28 '17 16:04

I.S


1 Answers

toList requires the sequence to terminate which doesn't happen on the outer stream that responds to text events. You should move the processing of the books into the switchMap:

textChangeSubscriber
        .map(CharSequence::toString) // <-- text components emit mutable CharSequence
        .debounce(300, TimeUnit.MILLISECONDS)
        .observeOn(Schedulers.computation())
        .switchMap(s -> 
              getItems(s)
              .flatMapIterable(items -> items)
              .map(Book::convert)
              .toList()
              .toFlowable() // or toObservable(), depending on textChangeSubscriber
        )
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(books -> {
            Log.i("test", "" + books.toString());
        }, error -> {
            Log.i("test", "" + error);
        });
like image 92
akarnokd Avatar answered Nov 06 '22 02:11

akarnokd