通常我用
Observable observable = someObservable.replay().refCount();
创建一个只能执行一次的可观察对象.如果我多次订阅可观察的内容,则效果很好,如下所示:
observable.observeOn(Schedulers.io())
.subscribeOn(AndroidSchedulers.mainThread())
.subscribe(...);
observable.observeOn(Schedulers.io())
.subscribeOn(AndroidSchedulers.mainThread())
.subscribe(...);
可观察对象仅执行一次-完美,正如我想要的那样.如果我现在执行以下操作:
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(...);
然后,可观察对象执行两次.我如何确保不会发生这种情况?
解决方法:
我认为您想要添加一个.take(1)运算符.这样,一旦从可观察对象读取了某些内容,它将调用onComplete.