Why the code below does not emit any result? What is going wrong in this Subject
? I would expect that on the moment I subscribe would get a result, namely the length I declare.
val subject = PublishSubject.create<String>()//PublishRelay.accept() does not work as well
subject.onNext("Alpha"
subject.onNext("Beta")
subject.onNext("Gamma")
subject.onComplete()
subject.map{ it.length }
.subscribe { println(it) }
Publish Subject only emits items after subscription. Try changing to ReplaySubject if you want all subscriptions to get all emissions, or BehaviourSubject if you want the last emissions when subscribing.
You can read more about different types of Subjects here:
http://reactivex.io/documentation/subject.html
val subject = ReplaySubject.create<String>()/
subject.onNext("Alpha"
subject.onNext("Beta")
subject.onNext("Gamma")
subject.onComplete()
subject.map{ it.length }
.subscribe { println(it) }
Alternatively, subscribe, then call onNext:
val subject = PublishSubject.create<String>()
subject.map{ it.length }
.subscribe { println(it) }
subject.onNext("Alpha"
subject.onNext("Beta")
subject.onNext("Gamma")
subject.onComplete()