Skip to content

Observable#repeatWhen #2889

Closed
Closed
@NachoSoto

Description

@NachoSoto

I'm trying to leverage the repeatWhen operator, but its behavior doesn't seem to be following the documentation.
I'm probably not using it correctly, but I can't see exactly what I'm doing wrong, and I also noticed that this operator is lacking test coverage.

final AtomicInteger i = new AtomicInteger(0);
final Observable<?> timer = Observable.timer(100, TimeUnit.MILLISECONDS)
        .take(6)
        .cache();

final Observable<Integer> result = Observable.defer(() -> Observable.just(i.getAndIncrement()))
        .repeatWhen(observable -> timer)
        .cache();

result
        .subscribe(System.out::println);

// wait for result to complete.
result
        .toList()
        .toBlocking()
        .first();

I would expect this to print 0, 1, 2, 3, 4, 5 and then complete, but instead result is only emitting 0 and completing.

Note that what I'm trying to accomplish is more complex than this (this example in particular could be implemented with timer() + map()).
In my example the observable returned from repeatWhen is a subject to which I send values to make the resulting Observable repeat itself, but I simplified this for illustration purposes.

Could somebody point to what I'm doing wrong, or whether there's a better way to implement what I described?

Thank you.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions