rxjava PublishSubject 第二次不订阅
rxjava PublishSubject second time not subscribe
private static PublishSubject<UserBean> mPublishSubject;
private static AtomicBoolean mRefreshing = new AtomicBoolean(false);
private static Observable<UserBean> mTokenObservable;
public Observable<UserBean> getNetTokenLocked() {
if (mRefreshing.compareAndSet(false, true)) {
Log.e(TAG, "first request");
mTokenObservable.subscribe(mPublishSubject);
} else {
Log.e(TAG, "else wait back");
}
return mPublishSubject; //second didn't return
}
return mPublishSubject; <- 我希望能够第二次给我以前的数据
Publish Subject 在订阅时发出源 Observable 的所有后续项。如果您想发出源 Observable 的所有项目,无论订阅者何时订阅,请使用 Replay Subject。
PublishSubject<Integer> source = PublishSubject.create();
// It will get 1, 2, 3, 4 and onComplete
source.subscribe(getFirstObserver());
source.onNext(1);
source.onNext(2);
source.onNext(3);
// It will get 4 and onComplete for second observer also.
source.subscribe(getSecondObserver());
source.onNext(4);
source.onComplete();
private static PublishSubject<UserBean> mPublishSubject;
private static AtomicBoolean mRefreshing = new AtomicBoolean(false);
private static Observable<UserBean> mTokenObservable;
public Observable<UserBean> getNetTokenLocked() {
if (mRefreshing.compareAndSet(false, true)) {
Log.e(TAG, "first request");
mTokenObservable.subscribe(mPublishSubject);
} else {
Log.e(TAG, "else wait back");
}
return mPublishSubject; //second didn't return
}
return mPublishSubject; <- 我希望能够第二次给我以前的数据
Publish Subject 在订阅时发出源 Observable 的所有后续项。如果您想发出源 Observable 的所有项目,无论订阅者何时订阅,请使用 Replay Subject。
PublishSubject<Integer> source = PublishSubject.create();
// It will get 1, 2, 3, 4 and onComplete
source.subscribe(getFirstObserver());
source.onNext(1);
source.onNext(2);
source.onNext(3);
// It will get 4 and onComplete for second observer also.
source.subscribe(getSecondObserver());
source.onNext(4);
source.onComplete();