-
Notifications
You must be signed in to change notification settings - Fork 0
Filter Operators : Filter
Devrath edited this page Dec 29, 2023
·
1 revision

- This operator helps you to receive only the
onNextemission of an element that satisfies the predicate condition that we specify. - It ignores all the other
onNextelements. - It also allows
onErrorandonCompleteemissions.
// Create the observable
private val filterSubject = PublishSubject.create<String>()
// Create the subscriber
private fun subscribeFilterAt(){
filterSubject.filter { it==episodeII }.subscribeBy(
onNext = { println("Element printed:-> $it") },
onError = { println("Error Triggered") },
onComplete = { println("Complete Triggered") }
)
}
fun filterDemo() {
// Subscribe the observer to the observable
subscribeFilterAt()
// Publish some elements
filterSubject.onNext(episodeI)
filterSubject.onNext(episodeII)
filterSubject.onNext(episodeIII)
// Indicate completion of stream
filterSubject.onComplete()
}Attack of the Clones
Complete Triggered