本文整理汇总了Java中io.reactivex.subjects.ReplaySubject.onNext方法的典型用法代码示例。如果您正苦于以下问题:Java ReplaySubject.onNext方法的具体用法?Java ReplaySubject.onNext怎么用?Java ReplaySubject.onNext使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.reactivex.subjects.ReplaySubject
的用法示例。
在下文中一共展示了ReplaySubject.onNext方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: doSomeWork
import io.reactivex.subjects.ReplaySubject; //导入方法依赖的package包/类
private void doSomeWork() {
ReplaySubject<Integer> source = ReplaySubject.create();
source.subscribe(getFirstObserver()); // it will get 1, 2, 3, 4
source.onNext(1);
source.onNext(2);
source.onNext(3);
source.onNext(4);
source.onComplete();
/*
* it will emit 1, 2, 3, 4 for second observer also as we have used replay
*/
source.subscribe(getSecondObserver());
}
示例2: doSomeWork
import io.reactivex.subjects.ReplaySubject; //导入方法依赖的package包/类
/**
* ReplaySubject会发射所有来自原始Observable的数据给观察者,无论它们是何时订阅的。
* 也有其它版本的ReplaySubject,在重放缓存增长到一定大小的时候或过了一段时间后会丢弃旧的数据(原始Observable发射的)。
*
* 如果你把ReplaySubject当作一个观察者使用,注意不要从多个线程中调用它的onNext方法(包括其它的on系列方法),
* 这可能导致同时(非顺序)调用,这会违反Observable协议, 给Subject的结果增加了不确定性。
*/
private void doSomeWork() {
ReplaySubject<Integer> source = ReplaySubject.create();
source.subscribe(getFirstObserver()); // it will get 1, 2, 3, 4
source.onNext(1);
source.onNext(2);
source.onNext(3);
source.onNext(4);
source.onComplete();
/*
* it will emit 1, 2, 3, 4 for second observer also as we have used replay
*/
source.subscribe(getSecondObserver());
}
示例3: testUnsubscribeBeforeEmit
import io.reactivex.subjects.ReplaySubject; //导入方法依赖的package包/类
@Test public void testUnsubscribeBeforeEmit() {
TestObserver<String> observer = new TestObserver<>();
ReplaySubject<String> subject = ReplaySubject.create();
SubscriptionProxy<String> proxy = SubscriptionProxy.create(subject);
proxy.subscribe(observer);
proxy.dispose();
observer.assertNotComplete();
observer.assertNoValues();
subject.onNext("Avanti!");
subject.onComplete();
// disposable observables may not be resused in RxJava2
observer = new TestObserver<>();
proxy.subscribe(observer);
observer.assertComplete();
observer.assertValue("Avanti!");
}
示例4: shouldCacheResultsWhileUnsubscribedAndDeliverAfterResubscription
import io.reactivex.subjects.ReplaySubject; //导入方法依赖的package包/类
@Test public void shouldCacheResultsWhileUnsubscribedAndDeliverAfterResubscription() {
TestObserver<String> observer = new TestObserver<>();
ReplaySubject<String> subject = ReplaySubject.create();
SubscriptionProxy<String> proxy = SubscriptionProxy.create(subject);
proxy.subscribe(observer);
proxy.dispose();
observer.assertNoValues();
subject.onNext("Avanti!");
subject.onComplete();
// disposable observables may not be resused in RxJava2
observer = new TestObserver<>();
proxy.subscribe(observer);
observer.awaitTerminalEvent(3, TimeUnit.SECONDS);
observer.assertValue("Avanti!");
}
示例5: shouldRedeliverSameResultsToDifferentSubscriber
import io.reactivex.subjects.ReplaySubject; //导入方法依赖的package包/类
@Test public void shouldRedeliverSameResultsToDifferentSubscriber() {
// Use case: When rotating an activity, ObservableManager will re-subscribe original request's
// Observable to a new Observer, which is a member of the new activity instance. In this
// case, we may want to redeliver any previous results (if the request is still being
// managed by ObservableManager).
TestObserver<String> observer = new TestObserver<>();
ReplaySubject<String> subject = ReplaySubject.create();
SubscriptionProxy<String> proxy = SubscriptionProxy.create(subject);
proxy.subscribe(observer);
subject.onNext("Avanti!");
subject.onComplete();
proxy.dispose();
TestObserver<String> newSubscriber = new TestObserver<>();
proxy.subscribe(newSubscriber);
newSubscriber.awaitTerminalEvent(3, TimeUnit.SECONDS);
newSubscriber.assertComplete();
newSubscriber.assertValue("Avanti!");
observer.assertComplete();
observer.assertValue("Avanti!");
}
示例6: shouldKeepDeliveringEventsAfterResubscribed
import io.reactivex.subjects.ReplaySubject; //导入方法依赖的package包/类
@Test public void shouldKeepDeliveringEventsAfterResubscribed() {
TestObserver<String> observer = new TestObserver<>();
ReplaySubject<String> subject = ReplaySubject.create();
SubscriptionProxy<String> proxy = SubscriptionProxy.create(subject);
proxy.subscribe(observer);
subject.onNext("Avanti 1");
proxy.dispose();
observer = new TestObserver<>();
proxy.subscribe(observer);
subject.onNext("Avanti!");
observer.assertValues("Avanti 1", "Avanti!");
}
示例7: main
import io.reactivex.subjects.ReplaySubject; //导入方法依赖的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");
}
};
ReplaySubject<Long> replaySubject=ReplaySubject.create();
replaySubject.onNext(1l);
replaySubject.onNext(2l);
replaySubject.subscribe(observer);
replaySubject.onNext(10l);
replaySubject.onComplete();
}