简体   繁体   中英

How to get emitted value from first observable

I have a flow like this

        Observable.fromIterable(configuration.symbols) // list of data (for ex. 0, 1, 2, 3)
                    .subscribeOn(Schedulers.newThread())
                    .observeOn(AndroidSchedulers.mainThread())
                    .flatMap {
api.anotherCall(
                                symbol = it) // emitted value 
                                .subscribeOn(Schedulers.newThread())
                                .observeOn(AndroidSchedulers.mainThread())
                    }
                    .subscribe { res ->
                        {
                            Ln.i(res) // result, but it would be perfect to know this data + symbol as a second param
                        }
                    }

Thanks for any help

ps Kotlin syntax

You have to use something else than flatMap like zip ( doc ).
With zip the two observable will be combined two by two.

Or you can continue to use flatMap but add a map on the second observable like:

api.anotherCall(symbol = it) // emitted value 
    .map { secondValue -> it to secondValue }
    .subscribeOn(Schedulers.newThread())
    .observeOn(AndroidSchedulers.mainThread())

Then the end observer will receive a Pair<> .

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