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.

refCount() with delay doesn't support multicasting

See original GitHub issue

RxJava 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:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
akarnokdcommented, Jan 21, 2020

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 for Observer2 because publish requires live audience while it emits its values.

0reactions
harisudhan7889commented, Jan 21, 2020

@akarnokd Thanks for the clarification.

Read more comments on GitHub >

github_iconTop 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 >

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