BehaviorSubject
(via RxRelay.BehaviorRelay
) を使用して、連続した からの最新の放出を保存しようとすると問題が発生しますObservable
。
「継続的」とは、Observable
基になるデータセットが変更されるたびにソースがデータを出力するように設計されていることを意味します。
はBehaviorSubject
source に登録されていますObservable
。
にサブスクライブすると、ソースから にBehaviorSubject
発行された最初の値しか受信しないように見えます。ソースはもはや継続的に放出していないように見え、実際、それ以上アイテムを放出しなくなりました。Observable
BehaviorSubject
Observable
というわけで、やや不自然な例を次に示します。
//A Singleton
public class DataManager {
private Observable<List<Item>> itemsObservable;
//A BehaviorRelay (BehaviorSubject)
public BehaviorRelay<List<Item>> itemsRelay = BehaviorRelay.create();
private DataManager() {
//An Observable which emits when subscribed, and then subsequently when the underlying uri's data changes
itemsObservable = SqlBrite.createQuery(Uri uri, ...);
//In practice I would lazily subscribe to the relay.
itemsObservable.subscribe(itemsRelay);
}
}
さて、BehaviorSubject
どこかから にサブスクライブします:
// Subscribe to the BehaviorSubject
DataManager.getInstance.itemsObservable.subscribe(items -> {
//Here, I would expect 'items' to be the most recent List<Item> emitted from the source Observable to the BehaviorSubject.
//However, it seems like it's only ever the *first* item emitted from the source Observable to the BehaviorSubject.
//It seems like the source Observable never emits to the BehaviorSubject more than once, despite the source's underlying
//dataset having changed (I am triggering this change in testing).
});