1
votes

I have an Observable, it is listening to database and emits item when it was added to db. When I subscribe to this observable it emits fast already stored items in db one by one. My question is could I create observable that will collect items that were emitted with certain interval (for example 100 millis) to the list and emit (or return in some function, like, doOnNext) whole list and separate items if there was emitted with bigger interval?

Thank in advance!

1

1 Answers

7
votes

You are looking for buffer operator:

Returns an Observable that emits buffers of items it collects from the source Observable. The resulting Observable emits connected, non-overlapping buffers, each of a fixed duration specified by the timespan argument.enter image description here

To emit collected items every 100 millis:

 dbObservable
     .buffer(100, TimeUnit.MILLISECONDS)
     ... // here is your Lists