BehaviorSubject in action

A BehaviorSubject will replay the latest or initial value in a next event to new subscribers. We will create a BehaviorSubject instance with an initial value of Test. We will start this section with a basic code setup and build on top of the previous example by just commenting out the sample code for PublishSubject:

executeProcedure(for: "BehaviorSubject"){    let disposeBag = DisposeBag()}

Then, we will create a subscription on it using subscribe(onNext:):

let behSubject = BehaviorSubject(value: "Test")    let initialSubscripton = behSubject.subscribe(onNext: {        print("Line number is \(#line) and value is" , $0)    })

We have used the line debugging identifier as an easy way to distinguish this printout in the console by ...

Get Reactive Programming with Swift 4 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.