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

此生再无相见时 提交于 2019-12-23 09:25:32

问题


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);
            });

回答1:


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);
        });


来源:https://stackoverflow.com/questions/43684734/why-debounce-with-tolist-doent-working-in-rxandroid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!