对搜索输入值进行去抖动处理,我想使用switchMap()
with 方法返回 Flowable<List<T>>
我已经使用 @Maxim Ostrovidov 建议编辑了我的代码,现在使用 debounce
我添加了 3 行,因为想要将列表转换为其他时间并接收列表,但不起作用。我在其他情况下使用了这 3 行,它可以工作,但不适用于 debounce
和switchMap
.flatMapIterable(items -> items)
.map(Product::fromApi)
.toList()
subscription = RxTextView.textChangeEvents(searchInput)
.toFlowable(BackpressureStrategy.BUFFER)
.debounce(400, TimeUnit.MILLISECONDS)
.observeOn(Schedulers.computation())
.switchMap(event -> getItems(searchInput.getText().toString()))
.flatMapIterable(items -> items)
.map(Product::fromApi)
.toList()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(/../);
最佳答案
因为没有 Observable.switchMapFlowable
运算符 yet ,您必须使用 toObservable
或 toFlowable
手动转换流(取决于您最终计划获得的流类型):
// Observable stream
RxTextView.textChangeEvents(searchInput)
.debounce(300, TimeUnit.MICROSECONDS)
.switchMap(event -> yourFlowable(event).toObservable())
...
// Flowable stream
RxTextView.textChangeEvents(searchInput)
.toFlowable(BackpressureStrategy.BUFFER) //or any other strategy
.debounce(300, TimeUnit.MICROSECONDS)
.switchMap(event -> yourFlowable(event))
...
关于android - 如何将 RxTextView switchMap 与 Flowable 数据一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43334099/