Artem
Artem

Reputation: 4639

RxJava PublishSubject+Debounce: the second item not emited

I want to use PublishSubject + debounce (in subscribe logic) for emit my items with delay. This is my code:

Subscription logic:

notificationSubject = PublishSubject.create<Notification>()
notificationSubject
            .debounce(300, TimeUnit.MILLISECONDS)
            .doOnIOSubscribeOnMain() // ext. fun, I hope you understand it
            .subscribe {
                displayNotification(it)
            }

And emit objects logic:

showNotification(obj1)
showNotification(obj2) 
// ...
fun showNotification(notification: Notification) {
    notificationSubject.onNext(notification)
}

But on subscribe I receive only first emitted item (obj1). And if I emit two objects (obj3, obj4) again I receive only first of emitted item (obj3).

How to fix it?

Upvotes: 1

Views: 2040

Answers (1)

akarnokd
akarnokd

Reputation: 69997

Debounce is a lossy operator that skips items emitted too close to each other. You can't use that for addressing your requirements.

You could zip with an interval instead:

notificationSubject.zipWith(Observable.interval(300, TimeUnit.MILLISECONDS), (a, b) -> a)

Upvotes: 2

Related Questions