2017-06-05 54 views
0

我需要消耗來自多個來源的事件。源的數量可能會有所不同,源可能會被添加或刪除。多個可觀察到的一個用戶

由於我希望有可能在不需要重新訂閱的情況下更改一組資源,因此merge不是。

試過類似

PublishSubject<Integer> sourcesSubject = PublishSubject.create(); 

sourcesSubject.subscribe(...); 

Observable<Integer> source1 = ...; 
source1.subscribe(sourcesSubject); // add first source 

Observable<Integer> source2 = ...; 
source2.subscribe(sourcesSubject); // add second source 

..... // How to remove a source? 

,但不知道如何刪除源。

有沒有簡單的解決方案?

+0

這味道像一個XY問題。你想要這個的原因是什麼? –

回答

0

有沒有發現刪除源最簡單的方法:

Observable<Integer> source = ...; 
Subscription subscription = source.subscribe(sourcesSubject); // add source 
subscription.unsubscribe(); // remove source 
1

我建議看看結合運營商如merge(和實例變種mergeWith)或concatconcatWith),這將消除需要使用Subject。當您取消訂閱時,所有來源將停止。

相關問題