refCount() with delay doesn't support multicasting
See original GitHub issueRxJava Version: 2.2.13
Suppose multiple observers subscribe with some delay between each subscription then refCount()
is not supporting Multicasting. Below is the sample for this scenario
Code Sample
val observable = Observable.range(1, 3)
.subscribeOn(Schedulers.io())
.map(object : Function<Int, Int> {
override fun apply(input: Int): Int {
println("Inside map operator Squaring the input")
return input * input
}
})
val refCountObservable = observable.publish().refCount()
println("Wait for 3 seconds for observer1 to subscribe")
Thread.sleep(3000)
refCountObservable.subscribe(observer1)
println("Wait for 3 seconds for observer2 to subscribe")
Thread.sleep(3000)
refCountObservable.subscribe(observer2)
Output
Wait for 3 seconds for observer1 to subscribe
Observer1 onSubscribe
Wait for 3 seconds for observer2 to subscribe
Inside map operator Squaring the input
Observer1 Output 1
Inside map operator Squaring the input
Observer1 Output 4
Inside map operator Squaring the input
Observer1 Output 9
Observer1 onComplete
Observer2 onSubscribe
Inside map operator Squaring the input
Observer2 Output 1
Inside map operator Squaring the input
Observer2 Output 4
Inside map operator Squaring the input
Observer2 Output 9
Observer2 onComplete
In the above output, you can see the map operator is called for the two observers instead of doing Multicasting.
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Using delay(0) in combination with refCount() - Stack Overflow
After that it will run asynchronous delay(0) ). And here: const m = source.multicast(() => new Subject<number> ...
Read more >Understanding RxJS Multicast Operators | by Netanel Basal
The refCount operator is based on reference counting; It looks at the number of current subscribers. If that number changes from zero to...
Read more >[Solved]-rxjs retry with delay and multicast-rx.js - appsloveworld
By using the refCount: true option, it ensures that when there are no more active subscribers, it will re-subscribe to the source observable,...
Read more >Understanding when and why to use multicasting operators ...
When all subscribers have unsubscribed it will unsubscribe from the source Observable. Share operator is same as using publish()+refCount() with a small ...
Read more >The magic of RXJS sharing operators and their differences
shareReplay() is not implemented with multicast but it uses a factory function internally and if we use it with refCount: true and ref...
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
Again,
refCount
starts over if the source completes. Timeout after a completed source has no effect. Even if it delayed a reconnection, you’d not get items forObserver2
becausepublish
requires live audience while it emits its values.@akarnokd Thanks for the clarification.