2

[RxJava version 3]

The link below shows how to pass variables along when chaining observables using nested observables:

In RxJava, how to pass a variable along when chaining observables?

However, it does not seem to work when the data all comes from a single observable. For example, in a data structure:

obs.flatMap(dataStructure ->
            obs.map(dataStructure1 ->
                    dataStructure1.b).map(b ->
                        foobar(dataStructure.a, b)));

Presumably this does not work because I am trying to take from the same observable twice, it processes all combinations of a and b twice. Apart from all that it seems ugly, is there a simple way to achieve what I want while being able to arbitrarily add things to the chain (e.g. do more operations to b before calling foobar)?

  • How is it not working? Do you want to avoid subscribing to `obs` multiple times? This does one round of combinations: `range(1, 10).flatMap(a -> range(1, 10).map(b -> println(a + ", " + b))).subscribe();`. – akarnokd Oct 15 '19 at 08:07
  • I'm not trying to get any combinations beyond those that already exist, In your example, I want `1, 1 2, 2 3, 3` etc. for a total of 10 prints. I am aware I could achieve this with `Observable range = Observable.range(1, 10); range.zipWith(range, (a, b) -> a + ", " + b) .subscribe(System.out::println);` However, I am asking if there is a way to do this with the nested observable approach rather than with zipWith. – Callum Forrester Oct 16 '19 at 09:37
  • I don't understand. With zip, you get `a.a` and `b.b` so what specific data access you try to get from some nesting? – akarnokd Oct 16 '19 at 15:08
  • Remember I said a single observable, so I don't want `a.a` and `b.b`, I want `a.a` and `a.b`. Though I am no longer sure if it matters in this context. I'm trying to process the 2 fields separately and then recombine them at the end using nested observables rather than ZipWith. So imperatively, this is simply `a = stream.next(); println(a.a + ", " + a.b)` in a for loop. – Callum Forrester Oct 16 '19 at 15:25
  • Process how? Why not simply call `obs.doOnNext(a -> println(a.a + ", " + a.b))` where you process an item from obs? Do you have dependent reactive processing for `a.a` and `a.b`? Perhaps some form of [async mapping](https://github.com/akarnokd/RxJavaExtensions#flowabletransformersmapasync)? – akarnokd Oct 16 '19 at 15:43
  • I achieved what I wanted with the following: `range(1, 10).flatMap(a -> just(a).map(/** do things to a **/).map(b -> a + ", " + b));` I'm not sure if that helps? – Callum Forrester Oct 29 '19 at 09:09
  • Makes no sense if you have synchronous inner in `flatMap`. – akarnokd Oct 29 '19 at 09:12
  • Is there a simpler way to achieve the desired output then? Like, let's say I did this: `range(1, 10).flatMap(a -> just(a).map(a -> a*a).map(b -> a + ", " + b));` Output would be: 1, 1 2, 4 3, 9 4, 16 etc. – Callum Forrester Oct 29 '19 at 11:42
  • Do a straightforward `map` with the expression: `range.map(a -> a + ", " + (a * a))`. – akarnokd Oct 29 '19 at 13:19
  • Have you tried this library? https://github.com/pakoito/Komprehensions It was created specially to simplify call chains. – 0x384c0 Nov 22 '19 at 05:25

0 Answers0