RX Java – share() and publish() What is the difference between autoconnect()?

In my opinion I thinkhare () and release () Autoconnect () is the same But in this code, the result is different

Observable<Integer> cold = Observable.create(subscriber -> {
        for (int i = 0; i <= 2; i++) {
            System.out.println("Hot Observable Emit " + i);
            subscriber.onNext(i);
        }
    });

    ConnectableObservable<Integer> connectble = cold.publish().autoConnect(2);
    //Obserable(Integer) connectavle = cold.share();
    connectble.subscribe(subscriber1);
    connectble.subscribe(subscriber2);

publish(). Autoconnect() output

Hot Observable Emit 0
Subscriber 1 : 0
Subscriber 2 : 0
....

Share() output

Hot Observable Emit 0
Subscriber 1 : 0
//the subscriber2 not receive event

In this case, can't we use share () for multicast in Rx Java? I found that conclusion is not suitable for this situation

Test environment: Oracle jdk1 8 rx-java 1.2 IDEA ubuntu

Solution

The difference between them is that when a subscriber unsubscribes from the share, the upstream is disconnected. When a new subscriber subscribes, the upstream source re subscribes and starts from scratch (if cold)

Autoconnect waits for a specified number of subscribers to connect once, and never disconnects when all subscribers unsubscribe This is a description of how it works

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>