Normally I use
Observable observable = someObservable.replay().refCount();
to create an observable, that is executed only once. This works fine if I subscribe to observable
multiple times like following:
observable.observeOn(Schedulers.io())
.subscribeOn(AndroidSchedulers.mainThread())
.subscribe(...);
observable.observeOn(Schedulers.io())
.subscribeOn(AndroidSchedulers.mainThread())
.subscribe(...);
The observable
is only executed once - perfect, just as I want it. If I now do following:
Observable obs1 = observable.map(...); // call some operators
Observable obs2 = observable.map(...); // call some operators
obs1.observeOn(Schedulers.io())
.subscribeOn(AndroidSchedulers.mainThread())
.subscribe(...);
obs2.observeOn(Schedulers.io())
.subscribeOn(AndroidSchedulers.mainThread())
.subscribe(...);
Then the observable
is executed twice. How can I make sure, that this won't happen?
I think what you want is to add a .take(1)
operator. This way once something is read from the observable it will call onComplete.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With