question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

2.x: repeat() / repeatWhen operator unexpected behavior

See original GitHub issue

I’m using Rxjava 2.1.2, where I try to use repeat operator to keep producing a sequence from a source publisher.

However, the repeat operator seems to repeat the stale sequence emitted by the source publisher when the publisher produced at the very beginning instead of recall the top upstream function inside that source publisher, which is used by the publisher to produce the sequence.

Example sample code:

class SourcePublisher() {
    var counter = 0
    val list1 = listOf<Int>(1, 3, 5, 7, 9)
    val list2 = listOf<Int>(0, 2, 4, 6, 8)

    fun getSource(): Single<List<Int>> {
        return if (counter++ % 2 == 0) Single.just(list1) else Single.just(list2)
    }
}

fun main(args: Array<String>) {
    val source = SourcePublisher()

    source.getSource()
	       .repeatWhen { completed -> completed.delay(5, TimeUnit.SECONDS) }
               .subscribe { list -> 
                println(list)
            }
}

...produced the followin output, every 5 seconds print out:
[1, 3, 5, 7, 9]
[1, 3, 5, 7, 9]
[1, 3, 5, 7, 9]
...

Excepted output, every 5 seconds following print out: 
[1, 3, 5, 7, 9]
[0, 2, 4, 6, 8]
[1, 3, 5, 7, 9]
[0, 2, 4, 6, 8]
...

I’m wondering is this an expected behavior for repeat operator, which means it will only keep repeating the very first sequence generated after publisher initialized? Or I misunderstand and use it in a wrong way to make it keep playing stale data?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
akarnokdcommented, Aug 16, 2017

Do you see that you call fun getSource() exactly once and its returned value is a constant flow?

You practically wrote

Single.just(list1).repeatWhen(...)

In order to get a fresh sequence on each repeat, you have to defer the subscription to the getSource() so it gets evaluated multiple times.

    Single.defer(() -> source.getSource()).repeatWhen(...)
2reactions
runzedongcommented, Aug 16, 2017

that fromCallable works like a charm. Thank you all.

Read more comments on GitHub >

github_iconTop Results From Across the Web

RepeatWhen in combination Observable.of(x) has unexpected ...
I am having unexpected behavior with Observable.of() and repeatWhen. I was wondering if this is correct behavior or not, and why?
Read more >
repeatWhen - RxJS
Returns an Observable that mirrors the source Observable with the exception of a complete . If the source Observable calls complete , this...
Read more >
Repeat operator - ReactiveX
The Repeat operator emits an item repeatedly. Some implementations of this operator allow you to repeat a sequence of items, and some permit...
Read more >
Always Know When to Use Share vs. ShareReplay - Bitovi
The way share and shareReplay work is not always obvious and might lead to unexpected behavior in your application.
Read more >
reactor/reactor - Gitter
Came across these 2 Stackoverflow questions: ... In fact, I was expecting repeat(Long, Predicate) will behave like repeating as long as predicate =...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found