June 2018
Intermediate to advanced
310 pages
6h 32m
English
The easiest way to understand Subject is that Subject = Observable + Observer.
On the one hand, it allows others to subscribe() to it. On the other, it can subscribe to other Observable:
val dataSource = Observable.fromIterable((1..3))val multicast = PublishSubject.create<Int>()multicast.subscribe { println("S1 $it")}multicast.subscribe { println("S2 $it")}dataSource.subscribe(multicast)Thread.sleep(1000)
The following code prints six lines, three for each subscriber:
S1 1S2 1S1 2S2 2S1 3S2 3
Note that we didn't use publish() on our dataSource, so it's cold. Cold means that each time somebody subscribes to this source, it will begin sending data anew. The hot Observable, on the other hand, doesn't have all the data, and will only ...
Read now
Unlock full access