Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hot and Cold observables - Disconnecting: Example output is not correct. #18

Open
huylv opened this issue Oct 29, 2015 · 3 comments
Open

Comments

@huylv
Copy link

huylv commented Oct 29, 2015

Hi,
Please check the code example here:

ConnectableObservable<Long> connectable = Observable.interval(200, TimeUnit.MILLISECONDS).publish();
Subscription s = connectable.connect();

connectable.subscribe(i -> System.out.println(i));

Thread.sleep(1000);
System.out.println("Closing connection");
s.unsubscribe();

Thread.sleep(1000);
System.out.println("Reconnecting");
s = connectable.connect();

The actual output is:

0
1
2
3
4
Closing connection
Reconnecting

Notice no more values appear after Reconnecting. Looks like calling unsubscribe on the ConnectableObservable's subscription also terminates subsequent subscriptions, too. I need to call connectable.subscribe(i -> System.out.println(i)) again in order to produce output while the text says that 'old observers will begin receiving values again' after calling connect() on the ConnectableObservable object. I'm using RxJava 1.0.14.

@Froussios
Copy link
Owner

Thanks for discovering this. It appears that the behaviour has changed since the example was written.

I think this may be a new bug in RxJava. Apart from the fact that I don't understand the logic behind such a change, the old subscriber isn't actually unsubscribed.

This

ConnectableObservable<Long> connectable =
    Observable.interval(200, TimeUnit.MILLISECONDS)
        .doOnNext(x -> System.out.println("BEFORE " + x))
        .publish();
Subscription s = connectable.connect();

Subscription consumerSubscription = connectable
    .doOnUnsubscribe(() -> System.out.println("AFTER unsubscribed"))
    .subscribe(i -> System.out.println(i));

Thread.sleep(1000);
System.out.println("Closing connection");
s.unsubscribe();

Thread.sleep(1000);
System.out.println("Reconnecting");
s = connectable.connect();

System.out.println("Unsubscribed: " + consumerSubscription.isUnsubscribed());
System.in.read();

produces

BEFORE 0
0
BEFORE 1
1
BEFORE 2
2
BEFORE 3
3
BEFORE 4
4
Closing connection
Reconnecting
Unsubscribed: false
BEFORE 0
BEFORE 1
BEFORE 2
BEFORE 3
...

The old subscriber isn't actually unsubscribed. It's just lost in the process.

@huylv
Copy link
Author

huylv commented Oct 30, 2015

Thanks for your explanation. There're several other errors as well but I don't recall for now. Will report when I see them again. Cheers.

@Froussios
Copy link
Owner

I ran the code against older versions of RxJava. It appears that the new behaviour is for 1.0.10 and newer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants