RxJava delay for each item of list emitted

后端 未结 15 1759
感情败类
感情败类 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:46

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

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

    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();
    
    0 讨论(0)
  • 2020-11-29 00:48
    Observable.just("A", "B", "C", "D", "E", "F")
        .flatMap { item -> Thread.sleep(2000)
            Observable.just( item ) }
        .subscribe { println( it ) }
    
    0 讨论(0)
  • 2020-11-29 00:49

    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

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

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