RxJava delay for each item of list emitted

后端 未结 15 1757
感情败类
感情败类 2020-11-29 00:14

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

相关标签:
15条回答
  • 2020-11-29 00:36

    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.

    0 讨论(0)
  • 2020-11-29 00:37

    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 讨论(0)
  • 2020-11-29 00:38

    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"

    0 讨论(0)
  • 2020-11-29 00:41

    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();
    
    0 讨论(0)
  • 2020-11-29 00:43

    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();
    
    0 讨论(0)
  • 2020-11-29 00:45

    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
    
    0 讨论(0)
提交回复
热议问题