How to emit items from a Collection with delay in RxJava?

Cihan Keser

I have the below code emitting items from a collection.

// fills the given coll with some items and
// returns the observable emitting the filled collection
// does ASYNC work
Observable<Collection> generate(Collection coll){ ... }

def coll = []
generate(coll).flatMap({ filledColl ->
    rx.Observable.from(filledColl)
}).subscribe({ ... })

The problem is that this collection can contain thousands of items and since generate works async, this code causes the subscribe method to be called thousands of times almost instantly (which is not wanted for the work I'm doing inside observer).

How can I modify this code to emit items from collection with a delay? For example: emit 100 items then wait 100ms then emit another 100 items or wait 10ms before emitting next item?

André Staltz

Inside the flatMap, you need to split the filledColl into smaller parts, delay each part, and merge them all into one observable which you will return inside the flatMap.

generate(coll).flatMap({ filledColl ->
    def chunkSize = 100
    resultStream = rx.Observable.never()
    for (i in 0 ..< filledCol.size()/chunkSize) {
        def chunk = filledCol[i*chunkSize .. (i+1)*chunkSize]
        resultStream = resultStream.mergeWith(
            rx.Observable.from(chunk).delay(100*i, TimeUnit.MILLISECONDS)
        )
    }
    resultStream
}).subscribe({ ... })

That's just the rough idea, you might still want to test, tweak and correct according to your needs. Also, it might make more sense to move this into the generate function, but that's up to you since I cannot know what is in generate().

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to emit items from a Collection with delay in RxJava?

From Dev

How to emit items from list with delay between each item?

From Dev

How to emit items from list with delay between each item?

From Dev

RxJava filter and emit other items

From Dev

RxJava2 emit items in order

From Dev

RxJava; How to emit observables synchronously

From Dev

Adding delay between Observable Items RxJava

From Dev

rxjava delay: How to get variable delay on each item emitted from a list?

From Dev

How To Add Items From a ComboBox to a List Collection

From Dev

rxJava create ArrayList from items

From Dev

Giving an RxJava Observable something to emit from another method

From Dev

How to flatten all items from a nested Java Collection into a single List?

From Dev

How to get ten, ten items from Observable Collection?

From Dev

How to take n random items from a collection in Clojure?

From Dev

Backbonejs collection.create: how to prevent it from adding items if there is an error?

From Dev

how to retrieve All Checked Items from radTreeListView SelectedItems Collection

From Dev

smalltalk: how to select the first n items from a collection

From Dev

How to use Linq to select items from a anonymous IEnumerable collection

From Dev

How do I publish two random items from a Meteor collection?

From Dev

Backbonejs collection.create: how to prevent it from adding items if there is an error?

From Dev

smalltalk: how to select the first n items from a collection

From Dev

How to transfer items from a ListBox to a collection VB.NET

From Dev

How do I publish two random items from a Meteor collection?

From Dev

C# - how to get distinct items from a Collection View

From Dev

How can I get all nested items from a collection?

From Dev

Accessing instance of generic collection from items in collection

From Dev

Updating Collection from other collection for matching items

From Dev

How to emit from a custom Directive?

From Dev

Exclude items from a collection in Laravel

Related Related

  1. 1

    How to emit items from a Collection with delay in RxJava?

  2. 2

    How to emit items from list with delay between each item?

  3. 3

    How to emit items from list with delay between each item?

  4. 4

    RxJava filter and emit other items

  5. 5

    RxJava2 emit items in order

  6. 6

    RxJava; How to emit observables synchronously

  7. 7

    Adding delay between Observable Items RxJava

  8. 8

    rxjava delay: How to get variable delay on each item emitted from a list?

  9. 9

    How To Add Items From a ComboBox to a List Collection

  10. 10

    rxJava create ArrayList from items

  11. 11

    Giving an RxJava Observable something to emit from another method

  12. 12

    How to flatten all items from a nested Java Collection into a single List?

  13. 13

    How to get ten, ten items from Observable Collection?

  14. 14

    How to take n random items from a collection in Clojure?

  15. 15

    Backbonejs collection.create: how to prevent it from adding items if there is an error?

  16. 16

    how to retrieve All Checked Items from radTreeListView SelectedItems Collection

  17. 17

    smalltalk: how to select the first n items from a collection

  18. 18

    How to use Linq to select items from a anonymous IEnumerable collection

  19. 19

    How do I publish two random items from a Meteor collection?

  20. 20

    Backbonejs collection.create: how to prevent it from adding items if there is an error?

  21. 21

    smalltalk: how to select the first n items from a collection

  22. 22

    How to transfer items from a ListBox to a collection VB.NET

  23. 23

    How do I publish two random items from a Meteor collection?

  24. 24

    C# - how to get distinct items from a Collection View

  25. 25

    How can I get all nested items from a collection?

  26. 26

    Accessing instance of generic collection from items in collection

  27. 27

    Updating Collection from other collection for matching items

  28. 28

    How to emit from a custom Directive?

  29. 29

    Exclude items from a collection in Laravel

HotTag

Archive