- 
                Notifications
    
You must be signed in to change notification settings  - Fork 0
 
Filter Operators : DistinctUntilChanged
        Devrath edited this page Dec 29, 2023 
        ·
        1 revision
      
    - This operator filters out the elements that are same as the previous emission
 
    private val distinctUntilChangedSubject = PublishSubject.create<String>()
    // Create the subscriber
    private fun subscribeDistinctUntilChanged(){
        distinctUntilChangedSubject.distinctUntilChanged().subscribeBy(
            onNext = { println("Element printed:-> $it") },
            onError = { println("Error Triggered") },
            onComplete = { println("Complete Triggered") }
        ).addTo(subscription)
    }
    fun distinctUntilChangedDemo() {
        // Subscribe the observer to the observable
        subscribeDistinctUntilChanged()
        // Publish some elements
        distinctUntilChangedSubject.onNext(episodeI)
        distinctUntilChangedSubject.onNext(episodeI)
        distinctUntilChangedSubject.onNext(episodeI)
        distinctUntilChangedSubject.onNext(episodeII)
        distinctUntilChangedSubject.onNext(episodeII)
        distinctUntilChangedSubject.onNext(episodeIII)
        distinctUntilChangedSubject.onNext(episodeIII)
        distinctUntilChangedSubject.onNext(episodeIII)
        // Indicate completion of stream
        distinctUntilChangedSubject.onComplete()
    }Element printed:-> The Phantom Menace
Element printed:-> Attack of the Clones
Element printed:-> Revenge of the Sith
Complete Triggered