本文整理匯總了Java中io.reactivex.subjects.BehaviorSubject.subscribe方法的典型用法代碼示例。如果您正苦於以下問題:Java BehaviorSubject.subscribe方法的具體用法?Java BehaviorSubject.subscribe怎麽用?Java BehaviorSubject.subscribe使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類io.reactivex.subjects.BehaviorSubject
的用法示例。
在下文中一共展示了BehaviorSubject.subscribe方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: doSomeWork
import io.reactivex.subjects.BehaviorSubject; //導入方法依賴的package包/類
/**
* 當觀察者訂閱BehaviorSubject時,它開始發射原始Observable最近發射的數據
* (如果此時還 沒有收到任何數據,它會發射一個默認值),然後繼續發射其它任何來自原始Observable的數據。
* 然而,如果原始的Observable因為發生了一個錯誤而終止,BehaviorSubject將不會發射任何數據,隻是簡單的向前傳遞這個錯誤通知。
*/
private void doSomeWork() {
BehaviorSubject<Integer> source = BehaviorSubject.create();
source.subscribe(getFirstObserver()); // it will get 1, 2, 3, 4 and onComplete
source.onNext(1);
source.onNext(2);
source.onNext(3);
/*
* it will emit 3(last emitted), 4 and onComplete for second observer also.
*/
source.subscribe(getSecondObserver());
source.onNext(4);
source.onComplete();
}
示例2: doSomeWork
import io.reactivex.subjects.BehaviorSubject; //導入方法依賴的package包/類
private void doSomeWork() {
BehaviorSubject<Integer> source = BehaviorSubject.create();
source.subscribe(getFirstObserver()); // it will get 1, 2, 3, 4 and onComplete
source.onNext(1);
source.onNext(2);
source.onNext(3);
/*
* it will emit 3(last emitted), 4 and onComplete for second observer also.
*/
source.subscribe(getSecondObserver());
source.onNext(4);
source.onComplete();
}
示例3: main
import io.reactivex.subjects.BehaviorSubject; //導入方法依賴的package包/類
public static void main(String[] args) {
// TODO Auto-generated method stub
Observer<Long> observer=new Observer<Long>() {
@Override
public void onComplete() {
// TODO Auto-generated method stub
System.out.println("It's Done");
}
@Override
public void onError(Throwable throwable) {
// TODO Auto-generated method stub
throwable.printStackTrace();
}
@Override
public void onNext(Long value) {
// TODO Auto-generated method stub
System.out.println(":"+value);
}
@Override
public void onSubscribe(Disposable disposable) {
// TODO Auto-generated method stub
System.out.println("onSubscribe");
}
};
BehaviorSubject<Long> behaviorSubject=BehaviorSubject.create();
behaviorSubject.onNext(1L);
behaviorSubject.onNext(2L);
behaviorSubject.onNext(10L);
behaviorSubject.onError(new Exception("You got an Exception"));
behaviorSubject.subscribe(observer);
behaviorSubject.onComplete();
}