2.x: repeat() / repeatWhen operator unexpected behavior
See original GitHub issueI’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:
- Created 6 years ago
- Comments:5 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Do you see that you call
fun getSource()
exactly once and its returned value is a constant flow?You practically wrote
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.that
fromCallable
works like a charm. Thank you all.