代码之家  ›  专栏  ›  技术社区  ›  Droid Teahouse

展平前在RxJava中设置变量

  •  0
  • Droid Teahouse  · 技术社区  · 7 年前

    可观察(<Foo>从调用getSomething()返回。Foo有一个列表,基本上就是我想要的数据。我需要过滤列表,以便将其展平。然而,我需要来自Foo的另一段数据来在subscribe中设置一个变量。onNext()。我要设置的变量是具有默认setter的kotlin类的成员。我确实试过return@map但这似乎阻止了排放,使过滤器无法使用。我确实在其他线程中查看了该对和嵌套的可观察建议,但没有发现它有帮助。我还查看了flatMapIterable的第二个重载版本,其中有一个Func2,但并没有给出限制。在这种情况下,我如何正确应用上述任何一项,或者是否有其他更简单的解决方案?

        service.getSomething()
        .subscribeOn(Schedulers.io())
    
                ---> here I need to set limit = Foo.data.limit 
    
                            .flatMapIterable { t -> t.data.results }
                            .filter({ s -> filterLogic(s) })
                            .toList()
       .observeOn(AndroidSchedulers.mainThread())
                            .subscribe(
                                    { c ->   //this is the list, not Foo
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   alex.dorokhov    7 年前

    您不需要使用Rx将列表展平,然后对其进行过滤。你可以用Kotlin的 列表滤器 功能。最终代码可以如下所示:

    service.getSomething()
        .subscribeOn(Schedulers.io())
        .map {
            Pair(Foo.data.results.filter(this::filterLogic), Foo.data.limit)
        }
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe { (filteredResults, limit) ->
        }