Could you show an example? In general blockingFirs...
# rx
g
Could you show an example? In general blockingFirst sounds bad and flatmap is standard way to do that
Idiomatic_nested_mapping_for_Observable_s_.kt
g
Ehhmm, no, I think your solution is completely wrong and work only by change and only for single thread. Maybe you should check some basic tutorials about RxJava and operatos
First, try to fix your example in general, forr exampl you try to change
val
r
the example was something i just wrote right away. I wanted to know how nested Observable dependencies were handled.
on our production app, a similar class was created for our domain layer, which combines different data entities into one class.
g
To nest observables use flatMap
As I understand your example you need something like this:
Copy code
val parents: Single<List<Parent>> = getParents()
return parents.toObservable().flatMapIterable { it }
    .flatMap { parent ->
        Observable.zip(
                getFirst(parent),
                getSecond(parent),
                BiFunction<List<First>, List<Second>, Parent> { f, s ->
                    parent.apply {
                        first = f
                        second = s
                    }
                })
    }
    .toList()
r
damn! that’s such an elegant solution! thanks 🙂
g
So you just flatMap each item of parent on each item your request both child and zip them together with parent
General rule: do not use side effects operators (like doOnNext) for such cases, use only operators that modify data stream
Also general rule, try to avoid mutable objects like in this case with parent
use two different objects instead: parent without children and create new Parent (maybe even different class) with children
something like that:
Copy code
val parentIds: Single<List<Int>> = getParentIds()
return parentIds.toObservable().flatMapIterable { it }
    .flatMap { parentId ->
        Observable.zip(
                getFirst(parentId),
                getSecond(parentId),
                BiFunction<List<First>, List<Second>, Parent> { f, s ->
                    Parent(parentId, f, s)
                })
    }
    .toList()
}
r
yes, definitely makes lot of sense. my purpose with the parent was to use it as a composition class. really learned a lot, thanks for taking out the time to explain 🙂
g
yes, it’s fine to compose, but avoid mutable classes, just create copy if you don’t want to mess up multithreading
🙂 1