如何使用连接的observable重放?它总是调用onComplete。我希望能够使用不同的值触发多个事件
例如:
Observable<String> interval = Observable.just("A", "B");
ConnectableObservable<String> published = interval.publish();
CompositeSubscription mCompositeSubscription = new CompositeSubscription();
mCompositeSubscription.add(published.subscribe(new Observer<String>() {
@Override
public void onCompleted() {
out.println("adsf");
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(String s) {
out.println(s);
}
}));
published.connect();
// Now I want it to reuse the Observer or Subscriber to get the new events
interval = Observable.just("C", "D");
published = interval.replay();
我不想不断新手订阅者这样做。我怎样才能做到这一点?
谢谢!
答案 0 :(得分:0)
看看Debenu knowledge base article。使用其中一个(取决于您的需要)而不是Observable.just
,publish
和connect
。
Subject<String,String> interval = ReplaySubject.create();
interval.onNext("A");
interval.onNext("B");
//use asObservable before exposing your subject
Observable<String> published = interval.asObserbale;
CompositeSubscription mCompositeSubscription = new CompositeSubscription();
mCompositeSubscription.add(published.subscribe(new Observer<String>() {
@Override
public void onCompleted() {
out.println("adsf");
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(String s) {
out.println(s);
}
}));
//send new values to Subcribers
interval.onNext("C");
interval.onNext("D");