2

BehaviorSubject(via RxRelay.BehaviorRelay) を使用して、連続した からの最新の放出を保存しようとすると問題が発生しますObservable

「継続的」とは、Observable基になるデータセットが変更されるたびにソースがデータを出力するように設計されていることを意味します。

BehaviorSubjectsource に登録されていますObservable

にサブスクライブすると、ソースから にBehaviorSubject発行された最初の値しか受信しないように見えます。ソースはもはや継続的に放出していないように見え、実際、それ以上アイテムを放出しなくなりました。ObservableBehaviorSubjectObservable

というわけで、やや不自然な例を次に示します。

//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).
});
4

1 に答える 1

1

私のテストに欠陥があったことがわかりました。をサポートする uriSqlBrite Observableは変更の通知を受けていObservableなかったため、 は新しい値を発行していませんでした。Subjectライフサイクルメソッドで再サブスクライブされたことに関係する少しの赤いニシンもありましたonResume..すべて意図したとおりに機能しています.

于 2016-11-10T09:14:13.990 に答える