在RxJava中,如何在链接可观察对象时传递变量? [英] In RxJava, how to pass a variable along when chaining observables?

查看:53
本文介绍了在RxJava中,如何在链接可观察对象时传递变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用RxJava链接异步操作,我想向下游传递一些变量:

I am chaining async operations using RxJava, and I'd like to pass some variable downstream:

Observable
   .from(modifications)
   .flatmap( (data1) -> { return op1(data1); })
   ...
   .flatmap( (data2) -> { 
       // How to access data1 here ?
       return op2(data2);
   })

这似乎是一种常见的模式,但是我找不到有关它的信息.

It seems like a common pattern but I couldn't find information about it.

推荐答案

我从Couchbase论坛获得的建议是使用嵌套的可观察对象:

The advice I got from the Couchbase forum is to use nested observables:

Observable
   .from(modifications)
   .flatmap( (data1) -> { 
       return op1(data1)
           ...
           .flatmap( (data2) -> { 
               // I can access data1 here
               return op2(data2);
           })
   });

我将其标记为可接受的答案,因为它似乎是最推荐的答案.如果处理太复杂而无法嵌套所有内容,则还可以使用函数调用检查解决方案.

I'll mark this as the accepted answer as it seems to be the most recommended. If your processing is too complex to nest everything you can also check the solution with function calls.

这篇关于在RxJava中,如何在链接可观察对象时传递变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆