These are chat archives for ReactiveX/RxJava

17th
Jun 2017
Ivan Schütz
@i-schuetz
Jun 17 2017 10:43
I'm confused about publish().refCount()... someone adviced me a while ago to use this to prevent the observable being called multiple times for each subscription, but it doesn't seem to work exactly like that. The only way I see right now is to call first publish(), to transform the observable in a connectable observable and call connect(), after all the observers subscribed
e.g.
public static void main(String[] args) {

    ConnectableObservable<String> source = Observable.just("foo").map(par -> {
        System.out.println("called!"); // executed only once
        return par;
    }).publish();

    source.subscribe(par ->
        System.out.println("o1: " + par)
    );

    source.subscribe(par ->
        System.out.println("o2: " + par)
    );

    source.connect();
}
Ivan Schütz
@i-schuetz
Jun 17 2017 10:49
this, on the other side...
public static void main(String[] args) {

    Observable<String> source = Observable.just("foo").map(par -> {
        System.out.println("called!"); // executed twice
        return par;
    }).publish().refCount();


    source.subscribe(par ->
            System.out.println("o1: " + par)
    );

    source.subscribe(par ->
            System.out.println("o2: " + par)
    );
}
for which cases is exactly publish().refCount() (or share()) of use?