简体   繁体   中英

How to convert Observable<Object> to Single <Object> in RxJava

I'm new for RxJava and here I'm trying to return Single object of Object class by running some test cases in list using flatMap . I'm trying to return the Single Object from Observable as mentioned below:

private fun isTrusted(configs: List<Configurations>): Single<Configurations> {
    return Observable.fromIterable(configs)
        .flatMapSingle {
            val rate = it.rate?.profit
            val result = rate?.toMap()?.let { it1 -> tests.testRules(it1) }
            if (result == true.toString()) logd("---- rules: $result")
            res.toSingle()
        }
          .toList()
        .map { list ->

            list.forEach { it->
                if (list.contains(MATCH_UNKNOWN)) {
                    test.isRulesUnknown = true
                }
            }
        }
}

Please help me if I'm doing something wrong.

You could use skipWhile to filter out non-matching elements then use firstOrError to get the first matching configuration as Single:

Observable.fromIterable(configs)
.skipWhile {
    val rate = it.rate?.profit
    val result = rate?.toMap()?.let { it1 -> tests.testRules(it1) }
    val found = result == true.toString()
    if (found) {
        logd("---- rules: $result")
    }
    !found
}
.firstOrError()

If you can define an empty Configurations, the last operator can be switched to

.first(Configurations.EMPTY)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM