1

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?

Artem
  • 4,569
  • 12
  • 44
  • 86
  • 2
    What's your requirement/problem really? debounce is not only for delay but also for filtering "rapid fire", though it would be the latest item that gets emitted, not the first. – laalto Jul 04 '18 at 07:32
  • @laalto I check via logs and I receive on subscribe only first emitted item. And is there debounce analogue without "rapid fire" filtering? – Artem Jul 04 '18 at 07:36
  • @laalto my requirement: I want to use PublishSubject + debounce (in subscribe logic) for emit my items with delay. – Artem Jul 04 '18 at 07:36
  • If you want delay without filtering, use the `delay()` operator. – laalto Jul 04 '18 at 07:36
  • @laalto delay cannot to distinct my items by time-intervals. It only delayed my items. – Artem Jul 04 '18 at 07:38

1 Answers1

2

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)
akarnokd
  • 69,132
  • 14
  • 157
  • 192