RxJava有Hot Observables,在你的情況下你需要使用主題,將一個可觀測值的發射傳遞給另一個。
檢查該單元測試
/**
* In this example we see how using hot observables ReplaySubject we can emit an item on broadcast to all the observers(subscribers).
*
* @throws InterruptedException
*/
@Test
public void testHotObservableUsingReplaySubject2() throws InterruptedException {
Observable<Long> interval = Observable.interval(100L, TimeUnit.MILLISECONDS);
Subject<Long, Long> publishSubject = ReplaySubject.create(1);
interval.subscribe(publishSubject);
Thread.sleep(1000L);
publishSubject.subscribe(System.out::println, (e) -> System.err.println(e.getMessage()), System.out::println);
}
你可以看到熱觀測量這裏https://github.com/politrons/reactive/blob/master/src/test/java/rx/observables/connectable/HotObservable.java
的更多示例