为什么 debounce() 和 toList() 在 RxAndroid 中不起作用? [英] Why debounce() with toList() doen't working in RxAndroid?

查看:39
本文介绍了为什么 debounce() 和 toList() 在 RxAndroid 中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 debounce() 时,然后从后端获取数据和数据我想转换为另一个数据并最后使用 toList().当我使用 toList() 时,什么都没有发生,没有任何日志不在订阅和错误中,没有 toList() 它可以工作并且 subscribe()方法输入尽可能多的书籍列表,我测试了代码的第二部分,没有 debounce() 只是 getItems() 并使用 toList() 它有效.下面是我的代码,第一部分 debounce()itList() 不起作用,第二部分 toList() 起作用

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

推荐答案

toList 要求序列终止,这不会发生在响应文本事件的外部流上.您应该将书籍的处理移动到 switchMap 中:

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

这篇关于为什么 debounce() 和 toList() 在 RxAndroid 中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆