【问题标题】:How to use RxTextView switchMap with Flowable data?如何将 RxTextView switchMap 与 Flowable 数据一起使用?
【发布时间】:2017-04-10 22:51:59
【问题描述】:

去抖动搜索输入值,我想使用switchMap() 和返回Flowable<List<T>> 的方法我已经使用@Maxim Ostrovidov 的建议编辑了我的代码,现在使用debounce 我添加了3 行,就像想去一样超过列表转换为其他时间并接收列​​表,但不起作用。我在其他情况下使用了这 3 行,但不适用于 debounceswitchMap

.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(/../);

【问题讨论】:

    标签: android rx-java observable rx-android rx-java2


    【解决方案1】:

    由于没有Observable.switchMapFlowable 运算符yet,您必须使用toObservabletoFlowable 手动转换您的流(取决于您最终计划获得的流类型):

    // 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))
        ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-04
      • 2021-04-27
      • 2021-07-29
      • 1970-01-01
      • 1970-01-01
      • 2023-02-22
      • 1970-01-01
      • 2021-12-20
      相关资源
      最近更新 更多