Subjects: Rxjava

PublishSubject:

val basicSubject: PublishSubject<String> = PublishSubject.create()
val observer = basicSubject.subscribe({
Log.d(TAG, "received string:= $it")
}, {
it
.printStackTrace()
})
basicPublishSubject.onNext("hello")
basicPublishSubject.onNext("Kotlin")
basicPublishSubject.onComplete()
//subject declaration 
val basicSubject: PublishSubject<String> = PublishSubject.create()
basicSubject.onNext("hello")//attach subscriber to it
val observer = basicPublishSubject.subscribe({
Log.d(TAG, "received string:= $it")
}, {
it
.printStackTrace()
})
basicPublishSubject.onNext("Kotlin")
val observableSource1 : Observable<String> =  Observable.just("1", "2", "3", "4")val basicSubject: PublishSubject<String> = PublishSubject.create()basicSubject.subscribe({
Log.d(TAG, "received string:= $it")
},{
it
.printStackTrace()
})
//pass this subject as a observer to subscribe methodobservableSource1.subscribe(basicSubject)

BehaviorSubject:

//behaviour Subject
val behaviorSubject: BehaviorSubject<String> = BehaviorSubject.create()
behaviorSubject.subscribe{ Log.d(TAG, "observer 1 received string:= $it") }behaviorSubject.onNext("hello")
behaviorSubject.onNext("there")
behaviorSubject.onNext("Kotlin")
behaviorSubject.subscribe { Log.d(TAG, "observer 2 received string:= $it") }behaviorSubject.subscribe { Log.d(TAG, "observer 3 received string:= $it") }

ReplaySubject:

AsyncSubject:

//Async Subject
val asyncSubject: AsyncSubject<String> = AsyncSubject.create()
asyncSubject.onNext("hello")
asyncSubject.onNext("there")
asyncSubject.onNext("Kotlin")
asyncSubject.onComplete()
asyncSubject.subscribe {
Log.d(TAG, "observer 1 received string:= $it")
}
asyncSubject.subscribe {
Log.d(TAG, "observer 2 received string:= $it")
}
asyncSubject.subscribe {
Log.d(TAG, "observer 3 received string:= $it")
}

UnicastSubject:

val unicastSubject: UnicastSubject<String> = UnicastSubject.create()
val observable = Observable.interval(300, TimeUnit.MILLISECONDS)
.map {
"${(it+1)*300} milliseconds"
}.subscribe(unicastSubject)

Thread.sleep(2000);

unicastSubject.subscribe { Log.d(TAG, "observer 1 received string:= $it") }
Thread.sleep(2000);

Working remotely as Android Developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to test your In-app updates before releasing an Android app to production

How to copy text to clipboard in Flutter — Flutter small explanations

How to open Bottom Sheet in Android Kotlin

Flutter List View

Delegating Delegates to Kotlin

Find Nearby ATMS with integration of Huawei Site Kit in Banking app (Kotlin)

Debugging live android applications

Navigation: Conditional Navigation

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Gaurav Rajput

Gaurav Rajput

Working remotely as Android Developer

More from Medium

The Genius of “IGOR” by Tyler, the Creator.

Journal Week of 4/5

Jomi vs The World: Are There Exemptions To The Saying “No One Is Above The Law”?

Two Tests: Getting What You Want Out of Live & Figuring Out What That Is.