I've an cold Observable
generated out of my control. When subscribed it always produce (if no errors):
onNext()
onCompleted()
I've an helper class like this:
public class Helper {
private Subscription subscription;
private ReplaySubject<MyData> subject;
private Observable<MyData> coldObservable;
public HelperClass(Observable<MyData> aColdObservable) {
coldObservable = aColdObservable;
subject = ReplaySubject.create(1);
triggerRefresh();
}
public Observable<MyData> getObservable() {
return subject.asObservable();
}
public void triggerRefresh() {
if (subscription != null) {
subscription.unsubscribe();
subscription = null;
}
subscription = coldObservable
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.computation())
.subscribe(subject);
}
}
I've multiple subscriptions to the subject, take this pseudo-code for client:
public class Client {
private final Helper helper;
private Observable<MyData> proxiedObservable;
public Client(Observable<MyData> coldObservable) {
helper = new Helper(coldObservable);
}
public void init() {
proxiedObservable = helper.getObservable()
.subscribeOn(Schedulers.io)
.observeOn(Schedulers.computation());
Subscription a = newSubscription("A");
Subscription b = newSubscription("B");
Subscription c = newSubscription("C");
}
public Subscription newSubscription(String name) {
return proxiedObservable.subscribe(data -> log("next " + name),
throwable -> log("error " + name),
() -> log("complete " + name));
}
public void refresh() {
helper.triggerRefresh();
}
}
at initialization this is printed in the log:
next A
complete A
next B
complete B
next C
complete C
at some point after that refresh()
is called and I would like this exact same log to be repeated, instead nothing is printed.
Apparently when the complete event is fired by the cold observer it automatically unsubscribe all the subscription to my proxiedObservable
.
I do not need the complete event, but I DO need the new MyData to reach all the subscriptions.
Is there a way to suppress the onComplete event? Or is there another way to achieve what I need here?