Understanding BehaviorSubject

What if we combine AsyncSubject and PublishSubject? Or mix the benefits of both? BehaviorSubject emits the last item it got before the subscription and all the subsequent items at the time of subscription while working with multicasting, that is, it keeps an internal list of Observers and relays the same emit to all of its Observers without replaying.

Here is the graphical representation which has been taken from ReactiveX documentation (http://reactivex.io/documentation/subject.html):

Let's modify the last example with BehaviorSubject and see what happens:

 fun main(args: Array<String>) { val subject = BehaviorSubject.create<Int>() ...

Get Reactive Programming in Kotlin now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.