简体   繁体   中英

RxJava 2: emit collected list of items after a certain period of time

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!

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. 在此处输入图片说明

To emit collected items every 100 millis:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM