Differences between map
and flatMap
of RxSwift
has been explained here. Now I want to observe the upper observable instance when the changes happen to it's inner observable. How can I do so?
Let's see the example,
func testFlatMap() {
let bag = DisposeBag()
struct Player {
var age: Int
var score: BehaviorSubject<Int>
}
let male = Player(age: 28, score: BehaviorSubject(value: 80))
let player = PublishSubject<Player>()
player.asObservable()
.flatMap { $0.score.asObservable() }
.subscribe(onNext: { print($0) })
.disposed(by: bag)
player.on(.next(male))
male.score.on(.next(100))
}
In this above example the output is,
80
100
as expected. But I want to know the the full Player object status (i.e. age
of the player) inside subscribe block .subscribe(onNext: { print($0) })
but it's only getting the score
. How can I do so?
My expected output is,
Player (where I can access both age:28 and score:80)
Player (where I can access both age:28 and score:100)