76

I'm struggling to implement something I assumed would be fairly simple in Rx.

I have a list of items, and I want to have each item emitted with a delay.

It seems the Rx delay() operator just shifts the emission of all items by the specified delay, not each individual item.

Here's some testing code. It groups items in a list. Each group should then have a delay applied before being emitted.

Observable.range(1, 5)
    .groupBy(n -> n % 5)
    .flatMap(g -> g.toList())
    .delay(50, TimeUnit.MILLISECONDS)
    .doOnNext(item -> {
        System.out.println(System.currentTimeMillis() - timeNow);
        System.out.println(item);
        System.out.println(" ");
    }).toList().toBlocking().first();

The result is:

154ms
[5]

155ms
[2]

155ms
[1]

155ms
[3]

155ms
[4]

But what I would expect to see is something like this:

174ms
[5]

230ms
[2]

285ms
[1]

345ms
[3]

399ms
[4]

What am I doing wrong?

2
  • wonder why isn't any of the answers actually answering the question. Why isn't this working, what's wrong with it?
    – eis
    Mar 15, 2018 at 6:46
  • 9
    "I'm struggling to implement something I assumed would be fairly simple in Rx" seems to be the introduction to every Rx question. :) Mar 5, 2019 at 14:54

17 Answers 17

79

The simplest way to do this seems to be just using concatMap and wrapping each item in a delayed Obserable.

long startTime = System.currentTimeMillis();
Observable.range(1, 5)
        .concatMap(i-> Observable.just(i).delay(50, TimeUnit.MILLISECONDS))
        .doOnNext(i-> System.out.println(
                "Item: " + i + ", Time: " + (System.currentTimeMillis() - startTime) +"ms"))
        .toCompletable().await();

Prints:

Item: 1, Time: 51ms
Item: 2, Time: 101ms
Item: 3, Time: 151ms
Item: 4, Time: 202ms
Item: 5, Time: 252ms
5
  • I like this approach much more, because I have no doubt that I should turn-off something. With zip+interval I think I need manually stop Interval emittion.
    – MercurieVV
    Mar 27, 2017 at 6:23
  • Works like a charm even when the upstream is slow. Jul 18, 2017 at 19:03
  • This seems like it would do fine when we know how many results we want. Is there a way to do this when we don't know how many there are? May 8, 2018 at 23:50
  • The concatMap step should work for any type of observable, bounded unbounded, sized and unsized. The example given is sized simply to match the question and ease of demonstration.
    – Magnus
    May 9, 2018 at 0:52
  • toCompletable is deprecated.
    – Johann
    Jun 11, 2019 at 8:48
68

One way to do it is to use zip to combine your observable with an Interval observable to delay the output.

Observable.zip(Observable.range(1, 5)
        .groupBy(n -> n % 5)
        .flatMap(g -> g.toList()),
    Observable.interval(50, TimeUnit.MILLISECONDS),
    (obs, timer) -> obs)
    .doOnNext(item -> {
      System.out.println(System.currentTimeMillis() - timeNow);
      System.out.println(item);
      System.out.println(" ");
    }).toList().toBlocking().first();
8
  • 2
    Thanks! I think the delay operator is not intended to be used the way I wanted to. This solution works :)
    – athor
    Oct 26, 2015 at 19:52
  • 16
    This won't work if you source observable emits items at a rate slower than 50 ms. The Observable.interval() will emit an item every 50 ms. the zip() operator will buffer these if there is no matching items from your grouped list. Then, when a group is emitted, zip will immediately combine that with an item from the interval observable and send it to your doOnNext() without delay.
    – kjones
    Oct 26, 2015 at 21:21
  • @kjones Agreed -- this will not work for a slow producer, only to delay one where the items are available at the same time. But the original question was - "I have a list of items, and I want to have each item emitted with a delay." Which sounds like from with a delay between each emission.
    – iagreen
    Oct 27, 2015 at 0:25
  • 1
    @iagreen - You're right. I just assumed the Observable.range(1,5) was a hypothetical source observable. Since pretty much everything I work on can produce items at any random time, I tend to project this requirement onto others as well.
    – kjones
    Oct 27, 2015 at 15:26
  • <!-- language: java --> Observable<Message> messageObservable = Observable .interval(1, TimeUnit.SECONDS) .map(i -> others.get(i.intValue())) .take(others.size()); Thats its more simple. Aug 5, 2016 at 18:07
49

Just sharing a simple approach to emit each item in a collection with an interval:

Observable.just(1,2,3,4,5)
    .zipWith(Observable.interval(500, TimeUnit.MILLISECONDS), (item, interval) -> item)
    .subscribe(System.out::println);

Each item will be emitted every 500 milliseconds

0
24

For kotlin users, I wrote an extension function for the 'zip with interval' approach

import io.reactivex.Observable
import io.reactivex.functions.BiFunction
import java.util.concurrent.TimeUnit

fun <T> Observable<T>.delayEach(interval: Long, timeUnit: TimeUnit): Observable<T> =
    Observable.zip(
        this, 
        Observable.interval(interval, timeUnit), 
        BiFunction { item, _ -> item }
    )

It works the same way, but this makes it reusable. Example:

Observable.range(1, 5)
    .delayEach(1, TimeUnit.SECONDS)
6

I think it's exactly what you need. Take look:

long startTime = System.currentTimeMillis();
Observable.intervalRange(1, 5, 0, 50, TimeUnit.MILLISECONDS)
                .timestamp(TimeUnit.MILLISECONDS)
                .subscribe(emitTime -> {
                    System.out.println(emitTime.time() - startTime);
                });
5

To introduce delay between each item emitted is useful:

List<String> letters = new ArrayList<>(Arrays.asList("a", "b", "c", "d"));

Observable.fromIterable(letters)
                .concatMap(item -> Observable.interval(1, TimeUnit.SECONDS)
                        .take(1)
                        .map(second -> item))
                .subscribe(System.out::println);

More good options at https://github.com/ReactiveX/RxJava/issues/3505

3

You can implement a custom rx operator such as MinRegularIntervalDelayOperator and then use this with the lift function

Observable.range(1, 5)
    .groupBy(n -> n % 5)
    .flatMap(g -> g.toList())
    .lift(new MinRegularIntervalDelayOperator<Integer>(50L))
    .doOnNext(item -> {
      System.out.println(System.currentTimeMillis() - timeNow);
      System.out.println(item);
      System.out.println(" ");
    }).toList().toBlocking().first();
1

To delay each group you can change your flatMap() to return an Observable that delays emitting the group.

Observable
        .range(1, 5)
        .groupBy(n -> n % 5)
        .flatMap(g ->
                Observable
                        .timer(50, TimeUnit.MILLISECONDS)
                        .flatMap(t -> g.toList())
        )
        .doOnNext(item -> {
            System.out.println(System.currentTimeMillis() - timeNow);
            System.out.println(item);
            System.out.println(" ");
        }).toList().toBlocking().first();
3
  • 1
    Unfortunately this doesn't work. The timestamp printed is the same for all items.
    – athor
    Oct 26, 2015 at 19:52
  • That's because you source observable range(1, 5) emits all items almost simultaneously. If your source was emitting items at random times you would see each group delay by 50 ms. The answer you have selected as the right answer will not do what you want if say your source emits 1 item, then delays for say 500 ms, then emits 4 more items. In that case the last 4 items will all have pretty much the same timestamp.
    – kjones
    Oct 26, 2015 at 21:12
  • yeah all executed at same time in doOnNext
    – Jemshit
    Aug 20, 2016 at 9:51
1

A not so clean way is to make the delay change with the iteration using the .delay(Func1) operator.

Observable.range(1, 5)
            .delay(n -> n*50)
            .groupBy(n -> n % 5)
            .flatMap(g -> g.toList())
            .doOnNext(item -> {
                System.out.println(System.currentTimeMillis() - timeNow);
                System.out.println(item);
                System.out.println(" ");
            }).toList().toBlocking().first();
1

There is other way to do it using concatMap as concatMap returns observable of source items. so we can add delay on that observable.

here what i have tried.

Observable.range(1, 5)
          .groupBy(n -> n % 5)
          .concatMap(integerIntegerGroupedObservable ->
          integerIntegerGroupedObservable.delay(2000, TimeUnit.MILLISECONDS))
          .doOnNext(item -> {
                    System.out.println(System.currentTimeMillis() - timeNow);
                    System.out.println(item);
                    System.out.println(" ");
                }).toList().toBlocking().first(); 
1

You can use

   Observable.interval(1, TimeUnit.SECONDS)
            .map(new Function<Long, Integer>() {
                @Override
                public Integer apply(Long aLong) throws Exception {
                    return aLong.intValue() + 1;
                }
            })
            .startWith(0)
            .take(listInput.size())
            .subscribe(new Consumer<Integer>() {
                @Override
                public void accept(Integer index) throws Exception {
                    Log.d(TAG, "---index of your list --" + index);
                }
            });

This code above not duplicate value(index). "I'm sure"

1
Observable.just("A", "B", "C", "D", "E", "F")
    .flatMap { item -> Thread.sleep(2000)
        Observable.just( item ) }
    .subscribe { println( it ) }
1

A Swift extension for both approaches suggested in this post.

Concat

import RxSwift

extension Observable {
    public func delayEach(_ dueTime: RxSwift.RxTimeInterval, scheduler: RxSwift.SchedulerType) -> RxSwift.Observable<Element> {
        return self.concatMap { Observable.just($0).delay(dueTime, scheduler: scheduler) }
    }
}

Zip

import RxSwift

extension Observable {
    public func delayEach(_ period: RxSwift.RxTimeInterval, scheduler: RxSwift.SchedulerType) -> RxSwift.Observable<Element> {
        return Observable.zip(
            Observable<Int>.interval(period, scheduler: scheduler),
            self
        ) { $1 }
    }
}

Usage

Observable.range(start: 1, count: 5)
    .delayEach(.seconds(1), scheduler: MainScheduler.instance)

My personal preference goes out to the concat approach since it will also work as expected when the upstream emits items at a slower rate than the delay interval.

And yes the original post is RxJava specific, but Google also brings you here for RxSwift queries.

1

Regarding eis' comment "wonder why isn't any of the answers actually answering the question. Why isn't this working, what's wrong with it?":

It's behaving differently than expected because delaying an item means its emission time is delayed relative to the time the item would otherwise be emitted - not relative to the previous item.

Imagine the OP's observable without any delay: All items are emitted in quick succession (in the same millisecond). With delay, each item is emitted later. But since the same delay is applied to each item, their relative emission times do not change. They are still emitted in one millisecond.

Think of a person entering the room at 14:00. Another person enters at 14:01. If you apply a delay of one hour to both, they enter at 15:00 and 15:01. There is still just one minute between them.

0

I think you want this:

Observable.range(1, 5)
            .delay(50, TimeUnit.MILLISECONDS)
            .groupBy(n -> n % 5)
            .flatMap(g -> g.toList())
            .doOnNext(item -> {
                System.out.println(System.currentTimeMillis() - timeNow);
                System.out.println(item);
                System.out.println(" ");
            }).toList().toBlocking().first();

This way it will delay the numbers going into the group rather than delaying the reduced list by 5 seconds.

2
  • Unfortunately that still doesn't accomplish what I need. The doOnNext method is still executed at the same time for all items.
    – athor
    Oct 23, 2015 at 0:05
  • use zip operator! combine your observable with interval observable! Feb 23, 2016 at 19:08
0

You can add a delay between emitted items by using flatMap, maxConcurrent and delay()

Here is an example - emit 0..4 with delay

@Test
fun testEmitWithDelays() {
    val DELAY = 500L
    val COUNT = 5

    val latch = CountDownLatch(1)
    val startMoment = System.currentTimeMillis()
    var endMoment : Long = 0

    Observable
        .range(0, COUNT)
        .flatMap( { Observable.just(it).delay(DELAY, TimeUnit.MILLISECONDS) }, 1) // maxConcurrent = 1
        .subscribe(
                { println("... value: $it, ${System.currentTimeMillis() - startMoment}") },
                {},
                {
                    endMoment = System.currentTimeMillis()
                    latch.countDown()
                })

    latch.await()

    assertTrue { endMoment - startMoment >= DELAY * COUNT }
}

... value: 0, 540
... value: 1, 1042
... value: 2, 1544
... value: 3, 2045
... value: 4, 2547
2
  • Isn't flatMap with maxConcurrent = 1 the same as concatMap? They won't interleave anyway.. Jun 13, 2018 at 14:05
  • @LookForAngular Of course, flatMap (..., maxConcurrent=1) behaves like concatMap.
    – cVoronin
    Jun 13, 2018 at 17:44
0

you should be able to achieve this by using Timer operator. I tried with delay but couldn't achieve the desired output. Note nested operations done in flatmap operator.

    Observable.range(1,5)
            .flatMap(x -> Observable.timer(50 * x, TimeUnit.MILLISECONDS)
                        .map(y -> x))
            // attach timestamp
            .timestamp()
            .subscribe(timedIntegers ->
                    Log.i(TAG, "Timed String: "
                            + timedIntegers.value()
                            + " "
                            + timedIntegers.time()));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.